Start learning free

LESSON 119 OF 289 · ARRAYS - CHALLENGES

Undo the Last Add in TypeScript: push and pop

Practice Read the full explanation

You keep a shopping list, an array of strings items. Add the word first to the end of the list, then add the word second to the end, then undo the last add by removing the last item. Return the list.

The undo always removes the item at the end of the list, even when the same word also appears earlier in the list.

 

Example 1:

Input: items = ["milk", "eggs"], first = "bread", second = "jam"
Output: ["milk", "eggs", "bread"]
Explanation: After the two adds the list is ["milk", "eggs", "bread", "jam"]. The undo removes "jam", the last item.

Example 2:

Input: items = [], first = "apples", second = "pears"
Output: ["apples"]
Explanation: The list starts empty. "apples" and "pears" are added, then "pears" is removed.

Example 3:

Input: items = ["water"], first = "juice", second = "water"
Output: ["water", "juice"]
Explanation: The list becomes ["water", "juice", "water"]. The undo removes the last item, the "water" just added, not the first one.

 

Constraints:

  • items has between 0 and 10 words: it may be empty.
  • first, second and every word in items are single words of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · ARRAYS - CHALLENGES

    Undo The Last Add in TypeScript, explained

    Problem overview

    The method undoLastAdd receives an array of strings, items, 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"] after the two adds. The undo removes "jam", so the result is ["milk", "eggs", "bread"]. With an empty list, "apples" and "pears", the result is ["apples"].

    Approach

    An array in TypeScript has a method for each end of the list. push adds a value after the last element, and pop removes the last element and returns it. Together they behave like a stack: the last thing added is the first thing taken back. The task is exactly that, so it is three method calls in the right order.

    The undo removes the item at the end, not the item with a certain value. That matters when the same word appears twice. With items = ["water"], first = "juice" and second = "water", the list is ["water", "juice", "water"] before the undo. pop removes the last "water", the one just added, and the result is ["water", "juice"].

    Step by step

    1. Push first. The word goes after every item already in the list. On an empty list it becomes the only item.
    2. Push second. Now second is the last item, because it was added last.
    3. Pop. This removes the last item, which is second, wherever else the same word appears.
    4. Return items. The list now ends with first.

    The solution in TypeScript

    class Solution {
        undoLastAdd(items: string[], first: string, second: string): string[] {
            items.push(first);
            items.push(second);
            items.pop();
            return items;
        }
    }
    

    A few details are worth knowing:

    • The array is changed in place. push and pop modify the array they are called on, and the method returns that same array. A caller who passed ["a"] and kept a reference sees the array become ["a", "b"] after undoLastAdd(items, "b", "c"), and the returned value is the very same object.
    • pop returns the removed item. Here the return value is ignored, which is fine. On an empty array pop returns undefined and changes nothing, but this lesson never pops from an empty array, because push has run twice first.
    • Types are only annotations. string[] tells the compiler what the array should hold. When TypeScript compiles to JavaScript the annotations are erased, so they check nothing while the program runs.

    Complexity

    Time: O(1). Adding to or removing from the end of an array takes constant time on average, and the method does three such operations. The cost does not depend on the length of the list.

    Space: O(1) extra. The method creates no new array. It works on the one it was given.

    Edge cases and common mistakes

    • Empty list. [] with "apples" and "pears" returns ["apples"].
    • Repeated words. Removing the word second by searching for its value would delete the first match, not the last item. Use pop, which always works on the end.
    • Same word twice in the adds. With first = "tea" and second = "tea", the list gets two "tea" items and the undo removes one, so one "tea" remains.
    • Adding the words in the wrong order. If second is pushed first, the undo removes first instead.
    • Forgetting the return. The array is changed in place, but the method's result is still what the tests read, so it must return items.

    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"].
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free