Problem overview
moveToFront receives a to-do list items, an array of strings, and a number index from 0 to the length minus 1. It returns the list with the item at index taken out of its place and put at index 0. All other items keep their order.
For ["wash", "shop", "cook", "read"] and index = 2 the item is "cook", so the answer is ["cook", "wash", "shop", "read"]. With ["tea", "coffee"] and index = 0 the item is already first, so nothing changes. In ["call", "pay", "call", "walk"] with index = 2, the "call" at index 2 moves and the other "call" stays behind it.
Approach
Do it in three moves: remember the item, remove it from its old place, and insert it at the front. Both changes are done with splice. Remembering the item first matters, because after the removal it is no longer in the array. Using the index, not the word, is what handles the repeated "call" correctly: only the item at that exact position moves.
Step by step
- Save the item. Store
items[index]in astringconstant nameditem. - Remove it.
items.splice(index, 1)starts atindexand removes one item. The items after it move one place to the left. - Insert it at the front.
items.splice(0, 0, item)starts at 0, removes nothing and addsitem. Every other item moves one place to the right. - Return
items. The others are still in their old order.
The solution in TypeScript
class Solution {
moveToFront(items: string[], index: number): string[] {
const item: string = items[index];
items.splice(index, 1);
items.splice(0, 0, item);
return items;
}
}
On the three examples it returns:
[ 'cook', 'wash', 'shop', 'read' ] [ 'tea', 'coffee' ] [ 'call', 'call', 'pay', 'walk' ]
Tracing the first example
Start with ["wash", "shop", "cook", "read"] and index = 2. Step 1 saves "cook". Step 2 removes it, leaving ["wash", "shop", "read"]. Step 3 puts it in at index 0, and "wash" and "shop" each move one place to the right, giving ["cook", "wash", "shop", "read"].
The type annotation string on item only documents the intent. If index were outside the array, items[index] would be undefined at run time whatever the annotation says, but the lesson's constraints keep it in range.
Details worth knowing
- The function changes the array it receives.
spliceworks in place, and the function returns that same array. Forl = ["a", "b", "c"], afterconst m = new Solution().moveToFront(l, 2)bothlandmprint[ 'c', 'a', 'b' ]andl === mistrue. The lesson's tests only look at the returned list. - Index 0. The item is removed and put straight back at 0, so the list is unchanged, which is the right answer.
- Single item. The only valid index is 0, which is the same case.
Testing
Try the last position, a single item and a two-item list: moveToFront(["a", "b", "c", "d"], 3) returns [ 'd', 'a', 'b', 'c' ], moveToFront(["solo"], 0) returns [ 'solo' ], and moveToFront(["x", "y"], 1) returns [ 'y', 'x' ].
Common mistakes
- Removing before saving. If the item is not stored first, it is lost with the removal.
- Inserting first. After
items.splice(0, 0, item), every item has moved one place to the right, so the old position is nowindex + 1. Removing atindexwould take out the wrong item. - Removing by value. Searching for the word with
indexOffinds the first "call", which may not be the one at the given index.
