
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Validating push pop sequence in JavaScript
Problem
JavaScript function that takes in two arrays, pushed and popped, as the first and the second argument. Both these arrays are guaranteed to consist of unique elements.
Our function should return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack, false otherwise.
For example, if the input to the function is −
const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1];
Then the output should be −
const output = true;
Output Explanation
We might do the following sequence −
push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example
The code for this will be −
const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1]; const validateSequence = (pushed = [], popped = []) => { let pushedIndex = 0 let poppedIndex = 0 const stack = [] while (pushedIndex < pushed.length) { if (stack[stack.length - 1] !== popped[poppedIndex]) { stack.push(pushed[pushedIndex++]) } else { stack.pop() poppedIndex += 1 } } while (stack.length) { if (stack.pop() !== popped[poppedIndex++]) { return false } } return true; }; console.log(validateSequence(pushed, popped));
Output
And the output in the console will be −
true
- Related Articles
- stack push() and pop() in C++ STL
- Push vs pop in stack class in C#
- queue::push() and queue::pop() in C++ STL
- Array push(), pop() and clear() functions in Ruby
- How to push and pop elements in a queue in Ruby?
- Validating a power JavaScript
- How to use Push-Location and Pop-Location command in PowerShell?
- C# Program to Implement Stack with Push and Pop operations
- Validating email and password - JavaScript
- Validating a password using JavaScript
- Validating brackets in a string in JavaScript
- Validating alternating vowels and consonants in JavaScript
- Program to check given push pop sequences are proper or not in python
- Validating a boggle word using JavaScript
- Validating a file size in JavaScript while uploading

Advertisements