Guest User

Untitled

a guest
Oct 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function removeItemFromStack(stack, item) {
  2. const tmp = stack.pop();
  3.  
  4. // if we've looked up the whole stack there's no item
  5. // just return tmp element back to the top and we're done
  6. if (stack.length === 0) {
  7. stack.push(tmp);
  8. return;
  9. }
  10.  
  11. // if the current top doesn't equal to item let's go further recursively
  12. // when the execution comes back don't forget to get back tmp to its origin place, e.g the current top of the stack
  13. if (tmp !== item) {
  14. removeItemFromStack(stack, item);
  15. stack.push(tmp);
  16. }
  17.  
  18. // at this point we have found the item
  19. // this means stop calling recursively and start getting back on functions call stack
  20. // the item will disappear because of we don't push it back
  21. }
Add Comment
Please, Sign In to add comment