Skip to content

Instantly share code, notes, and snippets.

@nhnam
Created January 8, 2022 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhnam/5a5ba2d900590fdd7c9da6ffce6dd7d5 to your computer and use it in GitHub Desktop.
Save nhnam/5a5ba2d900590fdd7c9da6ffce6dd7d5 to your computer and use it in GitHub Desktop.
// Complete the minimumSwaps function below.
function minimumSwaps(arr) {
var swaps = 0;
for(var i=0; i< arr.length; i++) {
while(arr[i] !== i + 1) {
let j = arr[i] - 1;
let tem = arr[i];
arr[i] = arr[j];
arr[j] = tem;
swaps ++;
}
}
return swaps;
}
let res = minimumSwaps([3, 7, 6, 9, 1, 8, 10, 4, 2, 5]);
res;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment