Start learning free

LESSON 118 OF 289 · ARRAYS - CHALLENGES

Undo the Last Add in C#: List Add and RemoveAt

Practice Read the full explanation

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

    C# · ARRAYS - CHALLENGES

    Undo The Last Add in C#, explained

    Problem overview

    UndoLastAdd receives a shopping list items (a List<string>) and two words, first and second. It adds both words to the end of the list, then undoes the last add by removing the final item, and returns the list.

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

    Approach

    A List<T> grows as you add to it, and Add always puts the new element at the end. So after the two adds, the last item is second, and removing the item at index Count - 1 takes away exactly what was added last.

    The same word can appear more than once. With items = ["water"], first = "juice" and second = "water", the list becomes water, juice, water. The undo has to remove the last water. That is why the lesson removes by index. Remove("water") would delete the first match, the wrong one.

    Step by step

    1. Add first with items.Add(first). On an empty list, it becomes the only item.
    2. Add second the same way. It is now the last item.
    3. Remove the last item with items.RemoveAt(items.Count - 1). Indexes start at 0, so the last valid index is one less than Count.
    4. Return items. The method changed the list in place, so the returned list is the same object that was passed in.

    The solution in C#

    class Solution {
        public List<string> UndoLastAdd(List<string> items, string first, string second) {
            items.Add(first);
            items.Add(second);
            items.RemoveAt(items.Count - 1);
            return items;
        }
    }

    Two details are worth knowing. Count is a property on a List<T>, written without parentheses (arrays use Length instead). And RemoveAt throws an ArgumentOutOfRangeException when the index is outside the list. That can't happen here, because the two adds guarantee at least two items.

    Tracing the list

    Take the third example, items = ["water"], first = "juice", second = "water":

    1. Start: water (Count is 1).
    2. After Add(first): water, juice (Count is 2).
    3. After Add(second): water, juice, water (Count is 3).
    4. RemoveAt(items.Count - 1) removes index 2, the final water: water, juice.

    The expression items.Count - 1 is evaluated when RemoveAt is called, after both adds, so it points at the item that was just added.

    The list is changed in place

    The method doesn't build a new list. Add and RemoveAt change the very list the caller passed in, because a List<string> is a reference type. After the call, the caller's variable shows the new contents, and the returned value is that same list. If you wanted to keep the original, you would copy it first with new List<string>(items) and work on the copy. This lesson doesn't ask for that.

    It also shows why the order of the two adds matters. Add appends at the end, so first goes in before second, and what stays after the undo is first at the end of the list.

    Edge cases and common mistakes

    • Removing by value. items.Remove(second) removes the first item equal to second, which is wrong when the word already appears earlier in the list.
    • Removing before adding. The order matters: add both words, then undo.
    • Using Length. Lists have Count. Length is for arrays and strings, and items.Length does not compile.
    • An empty list. It works with no special case, because both adds come first.

    Testing

    Results of the solution above:

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