Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function removeItemFromStack(stack, item) {
- const tmp = stack.pop();
- // if we've looked up the whole stack there's no item
- // just return tmp element back to the top and we're done
- if (stack.length === 0) {
- stack.push(tmp);
- return;
- }
- // if the current top doesn't equal to item let's go further recursively
- // when the execution comes back don't forget to get back tmp to its origin place, e.g the current top of the stack
- if (tmp !== item) {
- removeItemFromStack(stack, item);
- stack.push(tmp);
- }
- // at this point we have found the item
- // this means stop calling recursively and start getting back on functions call stack
- // the item will disappear because of we don't push it back
- }
Add Comment
Please, Sign In to add comment