Reorder an array according to given indexes

Run Settings
LanguageTypeScript
Language Version
Run Command
// https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/ // Given two integer arrays of same size, “arr[]” and “index[]”, reorder elements in “arr[]” // according to given index array. It is not allowed to given array arr’s length. const reorder = (arr, indices) => { if (arr.length !== indices.length) { return arr; } const tempList = new Array(arr.length); for (let i = 0; i < arr.length; i++) { tempList[indices[i]] = arr[i]; } for (let i = 0; i < tempList.length; i++) { arr[i] = tempList[i]; } }; var arr = ["a", "b", "c", "d", "e", "f"]; var indices = [2, 3, 4, 0, 5, 1]; reorder(arr, indices); console.log(arr); // logs: ["d", "f", "a", "b", "c", "e"]
Editor Settings
Theme
Key bindings
Full width
Lines