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
- Add
firstwithitems.Add(first). On an empty list, it becomes the only item. - Add
secondthe same way. It is now the last item. - Remove the last item with
items.RemoveAt(items.Count - 1). Indexes start at 0, so the last valid index is one less thanCount. - 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":
- Start:
water(Countis 1). - After
Add(first):water, juice(Countis 2). - After
Add(second):water, juice, water(Countis 3). RemoveAt(items.Count - 1)removes index 2, the finalwater: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 tosecond, 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 haveCount.Lengthis for arrays and strings, anditems.Lengthdoes not compile. - An empty list. It works with no special case, because both adds come first.
Testing
Results of the solution above:
| items | first | second | result |
|---|---|---|---|
["milk", "eggs"] | "bread" | "jam" | ["milk", "eggs", "bread"] |
[] | "apples" | "pears" | ["apples"] |
["water"] | "juice" | "water" | ["water", "juice"] |
