To reverse an array in-place, use two pointers, one at the start and one at the end of the array. Swap elements at these pointers and move towards the center.
- Initialize two pointers: one at the start and one at the end of the array.
- Swap the elements at these pointers.
- Move the pointers towards the center.
- Repeat until the pointers cross.
function reverseArray(arr) {
let start = 0;
let end = arr.length - 1;
while (start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]];
start++;
end--;
}
return arr;
}
// Example usage
console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]
console.log(reverseArray([10, 20, 30])); // Output: [30, 20, 10]
This method has a time complexity of O(n), where n is the length of the array.
Tags: basic, JavaScript, Arrays, Algorithm