Problem overview
The function moveToFront receives a to-do list items, a slice of strings, and a number index. It returns the list with the item at index moved to position 0. The other items keep their order.
For ["wash", "shop", "cook", "read"] and index = 2 the item is "cook". It goes to the front, and "wash" and "shop" each move one place to the right: [cook wash shop read]. If the index is 0, the item is already first and nothing changes. A word can appear more than once, so what moves is the item at that position, not the first copy of the word.
Approach
Moving an item is a removal followed by an insertion. Remove the item from its place, which closes the gap, then insert it at index 0, which pushes everything else one place right. The order of the others never changes. The removal destroys the item, so its value has to be saved first.
Trace the first example, index = 2. Save "cook". Deleting index 2 leaves [wash shop read]. Inserting "cook" at 0 gives [cook wash shop read]. Each step is easy to check on its own, which is why the lesson splits the work this way instead of shifting items by hand in a loop.
Step by step
- Save
items[index]in a variableitem. - Remove the item with
slices.Delete(items, index, index+1). The end index is not included, so this takes out exactly one item. Assign the result back toitems. - Insert
itemat index 0 withslices.Insertand assign the result back. - Return
items.
The solution in Go
func moveToFront(items []string, index int) []string {
item := items[index]
items = slices.Delete(items, index, index+1)
items = slices.Insert(items, 0, item)
return items
}
Both slices.Delete and slices.Insert return the changed slice, so each result is assigned back, just as with append. When index is 0, the item is deleted and put straight back in the same place, which is why that case needs no special code.
The functions work on the same memory as the slice that was passed in. After res := moveToFront(orig, 2) on ["a", "b", "c"], both orig and res print as [c a b]. A caller that wants to keep the old order should pass a copy, made with slices.Clone.
Testing
| items | index | result |
|---|---|---|
[wash shop cook read] | 2 | [cook wash shop read] |
[tea coffee] | 0 | [tea coffee] |
[call pay call walk] | 2 | [call call pay walk] |
These come from running the function. In the third row the word call appears twice, and the one at index 2 moves.
Edge cases and common mistakes
The constraints keep index between 0 and the length minus 1, so items[index] is always a real item. With an index outside that range the first line would panic with an index-out-of-range error, before anything is changed. A list with a single item works too: it is deleted and inserted back, and the result is the same one-item list.
- Forgetting to save the item. After the delete,
items[index]is a different item. Deleting index 2 from[wash shop cook read]leaves[wash shop read], and the wordcookis gone unlessitemholds it. - Inserting first, deleting second. After inserting at 0, every item has shifted, so the old index points to the wrong place. Inserting
cookat the front gives[cook wash shop cook read], and deleting index 2 then removesshopinstead of the duplicatecook. - Ignoring the return value. Without
items = ...the slice keeps its old length, and the change is lost or half done.
