Skip to content

Instantly share code, notes, and snippets.

@jayantasamaddar
Created May 23, 2022 13:22
Show Gist options
  • Save jayantasamaddar/79675461fa9eb5ad0ce8789e0456f744 to your computer and use it in GitHub Desktop.
Save jayantasamaddar/79675461fa9eb5ad0ce8789e0456f744 to your computer and use it in GitHub Desktop.
Swap two elements in an array located at different indexes
/* Swap - Using ES6 Array Destructuring */
const swap = (arr, indx1, indx2) => [arr[indx1], arr[indx2]] = [arr[indx2], arr[indx1]];
/* Swap - Pre-ES6 Version, using a temporary variable */
const swap2 = (arr, indx1, indx2) => {
const temp = arr[indx1];
arr[indx1] = arr[indx2];
arr[indx2] = temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment