Last active
August 27, 2022 15:42
-
-
Save Deityhub/68a507b96a9b1f1be669ca3a8d40c957 to your computer and use it in GitHub Desktop.
Reverse an array in place by swapping
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
function reverseArr(arr) { | |
const n = parseInt(arr.length/2); | |
for(let i = 0; i < n; i++) { | |
const temp = arr[i]; | |
arr[i] = arr[arr.length - i - 1]; | |
arr[arr.length - i - 1] = temp; | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment