Start learning free

LESSON 123 OF 677 · ARRAYS - CHALLENGES

Move to the Front in Scala

Practice Read the full explanation

You get a to-do list items, an ArrayBuffer 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 3
    Ctrl + Enter to check

    SCALA · ARRAYS - CHALLENGES

    Move To Front in Scala, explained

    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

    1. Remove by index and store what remove returns in a val named item. You need the returned item to put it back.
    2. Insert it at index 0. The index comes first, then the value. When index is 0, the item simply goes back where it was.
    3. 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 a String here, so insert(0, item) type-checks.
    • The method changes the buffer it receives. The caller's items and 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 at index + 1. Removing index takes out the wrong item. With ["wash", "shop", "cook", "read"] and index = 2, inserting first and then removing index 2 gives [cook, wash, cook, read]. Removing first avoids the shift.
    • Removing by value. items -= word removes the first item equal to the word, which with duplicates can be a different position than index. 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
    
    Keep going with Scala500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free