Problem overview
The method undoLastAdd receives a shopping list items (an array of strings) and two words, first and second. It adds first to the end of the list, adds second to the end, then undoes the last add by removing the last item. It returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes ["milk", "eggs", "bread", "jam"] and the undo removes "jam". The result is ["milk", "eggs", "bread"]. With an empty list, first = "apples" and second = "pears", the result is ["apples"].
Approach: a list used as a stack
Adding to the end and removing from the end are the two operations that arrays do best. push appends one or more values to the end of an array, and pop removes the last element and returns it. Used together, they make the array behave like a stack: the last thing added is the first thing removed. That is exactly what "undo the last add" means.
The lesson needs no loop and no condition. It needs three calls in the right order.
The same two methods work in the other direction at the front of an array: unshift adds at the start and shift removes the first element. Those are slower on long arrays, because every other element has to move over by one place, so the end of the array is the natural place to add and remove.
Step by step
- Push
first. It goes after every item already in the list. On an empty list it becomes the only item. - Push
second. It is now the last item, because it was added last. - Pop.
popdoesn't look at what the last item is. It removes the one at the end, which issecond. - Return
items. The array was changed in place, and after the undo it ends withfirst.
The solution in JavaScript
class Solution {
undoLastAdd(items, first, second) {
items.push(first);
items.push(second);
items.pop();
return items;
}
}
Details worth knowing:
pushandpopchange the array itself. They don't build a new one. The function returns the same array object it received, so the caller's variable sees the changes too. Afterconst list = ["a"]andconst result = new Solution().undoLastAdd(list, "b", "c"), the expressionresult === lististrueandlistis["a", "b"].popreturns the removed element. This solution ignores the returned value, which is fine here because the lesson only needs the list.- Calling
popon an empty array returnsundefinedand leaves the array empty. That can't happen in this lesson, since two words have just been pushed.
Edge cases and common mistakes
- The same word earlier in the list. With
items = ["water"],first = "juice"andsecond = "water", the list becomes["water", "juice", "water"]. The undo removes the last"water", the one just added, and the result is["water", "juice"]. Code that searches forsecondand removes the first match would delete the wrong item.popnever searches. - An empty starting list. No special case is needed: the two pushes work on an empty array, and the pop leaves
firstbehind. - Swapping the pushes. If
secondis pushed beforefirst, the pop removesfirstand the list ends with the wrong word. - Using
shiftinstead ofpop.shiftremoves the first element, not the last.
Testing
items = ["milk", "eggs"],first = "bread",second = "jam"gives["milk", "eggs", "bread"].items = [],first = "apples",second = "pears"gives["apples"].items = ["water"],first = "juice",second = "water"gives["water", "juice"].
