Problem overview
The method moveToFront gets a to-do list items, an ArrayBuffer[String], and a number index. It returns the list with the item at index moved to the front. The other items keep their order.
For ["wash", "shop", "cook", "read"] and index = 2, the item is "cook". The result is ["cook", "wash", "shop", "read"]: "wash" and "shop" each move one place right. For ["tea", "coffee"] and index = 0 the item is already first, so nothing changes. The list may hold the same word twice, as in ["call", "pay", "call", "walk"] with index = 2. Position is what counts: the "call" at index 2 moves, giving ["call", "call", "pay", "walk"].
Approach
Two calls do the whole job: take the item out of its place, then put it in at index 0. remove(index) deletes the item at that index and returns it, and the items behind it close the gap. insert(0, item) then adds the item at the front, and everything shifts right by one.
The buffer is changed in place, and the same buffer is returned.
Step by step
- Remove by index and store what
removereturns in avalnameditem. You need the returned item to put it back. - Insert it at index 0. The index comes first, then the value. When
indexis 0, the item simply goes back where it was. - Return
items.
Tracing the first example
Start with items = [wash, shop, cook, read] and index = 2. After remove(2) the call returns "cook" and the buffer is [wash, shop, read]: "read" slid left to close the gap. After insert(0, item) the buffer is [cook, wash, shop, read]: "wash" and "shop" slid right to make room. Every other item kept its relative order, which is what the task asks for. Because the item is held in item between the two calls, it is never lost.
The solution in Scala
class Solution {
def moveToFront(items: ArrayBuffer[String], index: Int): ArrayBuffer[String] = {
val item = items.remove(index)
items.insert(0, item)
return items
}
}
Notes on the Scala:
remove(index)removes by index and returns the removed element. The result is aStringhere, soinsert(0, item)type-checks.- The method changes the buffer it receives. The caller's
itemsand the returned value are the same object. - Both calls are plain method calls on a mutable buffer; there is no copy of the list.
Edge cases and common mistakes
- Inserting first, then removing. Trying
items.insert(0, items(index))and then removing is easy to get wrong: after the insert, the original is atindex + 1. Removingindextakes out the wrong item. With["wash", "shop", "cook", "read"]andindex = 2, inserting first and then removing index 2 gives[cook, wash, cook, read]. Removing first avoids the shift. - Removing by value.
items -= wordremoves the first item equal to the word, which with duplicates can be a different position thanindex. The task is about a position. - A one-item list. The item is removed and put straight back; the list is unchanged.
- Using
-=on duplicates. For[call, pay, call, walk],items -= "call"leaves[pay, call, walk]: it removes the first "call", not the one at index 2.
Another way
A new buffer can be built with +:, which puts an item in front: items.remove(2) +: items gives ArrayBuffer(cook, wash, shop, read) for the first example. It leaves the original buffer (now without "cook") as it is and returns a different buffer. The reference solution keeps working on one buffer, which matches the lesson's goal of practising remove and insert.
Testing
[wash, shop, cook, read], index = 2 -> cook wash shop read
[tea, coffee], index = 0 -> tea coffee
[call, pay, call, walk], index = 2 -> call call pay walk
