Created
May 23, 2022 13:22
-
-
Save jayantasamaddar/79675461fa9eb5ad0ce8789e0456f744 to your computer and use it in GitHub Desktop.
Swap two elements in an array located at different indexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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