frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

503 - Reorder Array with New Indexes - javascript

Open jsartisan opened this issue 2 months ago • 0 comments

index.js

export function reorderArray(items, newOrder) {
  for(let i = 0; i < newOrder.length; i++) {
   while(newOrder[i] !== i) {
     swap(items, i, newOrder[i])
     swap(newOrder, i, newOrder[i])
   }
  }
}
function swap(arr, i, j) {
  [arr[i], arr[j]] = [arr[j], arr[i]]
}

jsartisan avatar Sep 27 '25 13:09 jsartisan