You are viewing a single comment's thread. Return to all comments →
JavaScript solution:
function minimumSwaps(arr) { let sortedArray = [...arr].sort((a,b) => (a-b)); let unsortedArray = [...arr]; let holder; let counter = 0; while (sortedArray.join() !== unsortedArray.join()) { for (let i = 0; i < unsortedArray.length; i++) { if (unsortedArray[i] !== (i+1)) { holder = unsortedArray[i]; unsortedArray[i] = unsortedArray[holder - 1]; unsortedArray[holder - 1] = holder; counter++; } } } return counter; }
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
JavaScript solution: