Start learning free

LESSON 123 OF 671 · ARRAYS - CHALLENGES

Move an Item to the Front in Go

Practice Read the full explanation

You get a to-do list items, a slice of strings, and a number index. Return the list with the item at index moved to the front.

Take the item out of its place and put it at index 0. The other items keep their order.

 

Example 1:

Input: items = ["wash", "shop", "cook", "read"], index = 2
Output: ["cook", "wash", "shop", "read"]
Explanation: The item at index 2 is "cook". It moves to the front.
Then "wash" and "shop" move one place to the right.

Example 2:

Input: items = ["tea", "coffee"], index = 0
Output: ["tea", "coffee"]
Explanation: The item at index 0 is already at the front.
Nothing changes.

Example 3:

Input: items = ["call", "pay", "call", "walk"], index = 2
Output: ["call", "call", "pay", "walk"]
Explanation: The "call" at index 2 moves to the front.
The other "call" and "pay" move one place to the right.

 

Constraints:

  • items has 1 to 10 words.
  • index is from 0 to the length of items minus 1.
  • Every word is made of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 4
    Ctrl + Enter to check

    GO · ARRAYS - CHALLENGES

    Move To Front in Go, explained

    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

    1. Save items[index] in a variable item.
    2. 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 to items.
    3. Insert item at index 0 with slices.Insert and assign the result back.
    4. 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

    itemsindexresult
    [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 word cook is gone unless item holds it.
    • Inserting first, deleting second. After inserting at 0, every item has shifted, so the old index points to the wrong place. Inserting cook at the front gives [cook wash shop cook read], and deleting index 2 then removes shop instead of the duplicate cook.
    • Ignoring the return value. Without items = ... the slice keeps its old length, and the change is lost or half done.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free