Problem overview
The method moveToFront receives a to-do list items, an array of strings, and a number index. It returns the list with the item at index moved to the front. The item is taken out of its place and put at index 0, and all other items keep their order. The constraints say the list has 1 to 10 words, index is a valid position, and the same word may appear more than once.
The examples:
["wash", "shop", "cook", "read"]withindex = 2gives["cook", "wash", "shop", "read"]. "cook" moves to the front, and "wash" and "shop" shift right.["tea", "coffee"]withindex = 0stays["tea", "coffee"], since the item is already first.["call", "pay", "call", "walk"]withindex = 2gives["call", "call", "pay", "walk"]. The "call" at index 2 moves, and the other "call" is a different item.
Approach
Moving an item is two changes: take it out of its current place, then put it in at the front. The array method splice(start, count, item) does both. With a count of 1 and no items, it removes one item at start. With a count of 0 and an item, it inserts that item at start.
Once the item is removed, you no longer have it in the array, so save it first. The order is: save, remove, insert, return.
Step by step
- Save the item at
index,items[index], in a variable nameditem, declared withlet. The next step removes it, so you need your own copy. - Remove it with
splice: start atindexand remove 1 item. The items after it move one place to the left. - Put
itemat index 0 withsplicetoo: start at 0, remove 0 items, and additem. Every other item moves one place to the right. - Return
items.
The solution in JavaScript
class Solution {
moveToFront(items, index) {
let item = items[index];
items.splice(index, 1);
items.splice(0, 0, item);
return items;
}
}
Details worth knowing
splice changes the array in place, so the method edits the array it was given and returns that same array. The caller sees the change in their own variable too. The method does not make a copy.
When index is 0, the item is removed from the front and put straight back at the front, so the list ends up as it began. The code needs no check for that case.
Equal words are no problem. The code works with a position, not with a value, so in the third example only the "call" at index 2 moves.
Testing
Running the method on the three examples prints:
[ 'cook', 'wash', 'shop', 'read' ]
[ 'tea', 'coffee' ]
[ 'call', 'call', 'pay', 'walk' ]
Edge cases and common mistakes
- Inserting before removing. If you insert first, every item from position 0 on shifts right, and the old index now points at a different item. Removing at the old index then takes out the wrong word. On
["wash", "shop", "cook", "read"]withindex = 2, inserting "cook" at the front and then removing at index 2 gives[ 'cook', 'wash', 'cook', 'read' ]: "shop" is lost and "cook" is there twice. Remove first, or remove atindex + 1. - Forgetting to save the item. After the removal,
items[index]is the next word, not the one you moved. - Using the return value as the list.
splicereturns an array of the removed items, not the changed array. Returnitems. - Looking up by value. Searching for the word and removing the first match would move the wrong "call" when words repeat.
