Reorder an array in place by a list of new indexes
Problem
A drag-and-drop list sends back where each item ended up: newOrder[i] is the new index of the item that was at index i. Write reorder(items, newOrder) that moves every item to its new index in place and returns items.
const items = ["A", "B", "C", "D", "E", "F"];
reorder(items, [1, 5, 4, 3, 2, 0]);
// "A" moves to 1, "B" to 5, "C" to 4, "D" stays at 3, "E" to 2, "F" to 0
items; // ["F", "A", "E", "D", "C", "B"]Ref. UR-1418 · © UIRound · uiround.com/questions/reorder-an-array-in-place-by-a-list-of-new-indexes
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 5
- edge cases
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Copying into a new array and copying back is easy but uses O(n) extra space. Think about what happens if you swap an item straight into the slot where it belongs.
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.