Start learning free

LESSON 119 OF 285 · ARRAYS - CHALLENGES

Undo the Last Add in Go: append and Re-slicing

Practice Read the full explanation

You keep a shopping list, a slice 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

    GO · ARRAYS - CHALLENGES

    Undo The Last Add in Go, explained

    Problem overview

    The function undoLastAdd receives a slice of strings, items, and two words, first and second. It adds first to the end, then adds second to the end, then undoes the last add by removing the last item, and returns the list.

    With items = ["milk", "eggs"], first = "bread" and second = "jam", the list grows to ["milk", "eggs", "bread", "jam"] and the undo drops "jam". The result is ["milk" "eggs" "bread"]. With an empty list, "apples" and "pears", the result is ["apples"].

    The undo removes the item at the end, by position, not by value. With items = ["water"], first = "juice" and second = "water" the list becomes ["water", "juice", "water"], and the result is ["water" "juice"]: the "water" that goes is the last one, not the first.

    Approach

    Go slices have no push or pop methods. Two built-in tools do the work:

    • append(s, x) returns a slice with x added at the end.
    • A slice expression s[a:b] returns the part of s from index a up to, but not including, index b.

    Removing the last item means keeping everything before the last index: items[:len(items)-1]. Since the cut is made by position, it doesn't matter whether the same word appears earlier.

    Step by step

    1. Add first. Call append(items, first) and assign the result back to items. The assignment is required: append doesn't change the variable you pass in, it returns the longer slice.
    2. Add second the same way. Now second is the last item.
    3. Undo. Re-slice from the start up to len(items)-1, which leaves out the last item, and assign that back to items.
    4. Return items. It ends with first.

    The solution in Go

    func undoLastAdd(items []string, first string, second string) []string {
        items = append(items, first)
        items = append(items, second)
        items = items[:len(items)-1]
        return items
    }
    

    Two details are worth knowing:

    • An empty or nil slice works. append on an empty slice, or on a nil one, simply creates the first element. Calling undoLastAdd(nil, "a", "b") returns ["a"].
    • Slices share memory. A slice is a window onto an array. If the array behind items has spare room, append writes into it instead of copying. With a caller's slice of length 2 and capacity 10, the function returns a slice of length 3, while the caller's own slice still has length 2, but the array behind it now holds [milk eggs bread jam] in its first four positions. When the capacity is exactly the length, append copies into a new array, and the caller's slice stays [milk eggs]. The returned list is the same in both cases.

    If you want the removed word as well, read it before cutting: last := items[len(items)-1]. This lesson only needs the shorter list.

    Edge cases and common mistakes

    • Dropping the result of append. Writing append(items, first) on its own line is rejected by the compiler because its result is not used, and keeping the old items instead would lose the new word.
    • Removing by value. Searching for second and deleting it removes the wrong item when the word is also earlier in the list, as in the "water" example.
    • Using items[:len(items)]. The end index is exclusive, so that keeps everything. Subtract 1.
    • Negative index. Go has no items[-1]. The last index is len(items)-1.

    Testing

    • ["milk", "eggs"], "bread", "jam" gives ["milk" "eggs" "bread"].
    • [], "apples", "pears" gives ["apples"].
    • ["water"], "juice", "water" gives ["water" "juice"].
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free