Rotate an array by k positions in JavaScript
This post will discuss how to rotate an array by k positions in JavaScript.
There are several ways to rotate an array in JavaScript, depending on the direction and the number of times we want to rotate the elements. Rotating an array means shifting the elements to the left or right and putting them back on the opposite end of the array. For example, if we have an array [1, 2, 3, 4] and we want to rotate it to the right by one position, we will get [4, 1, 2, 3]. Some of the common functions to rotate an array in JavaScript are:
1. Using pop() and unshift() functions
These functions allow us to remove the last element of an array and add it to the beginning, or vice versa. We can use them in a loop to rotate the array by a given number of times. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// rotate the array to the right by k positions function rotateRight(arr, k) { for (let i = 0; i < k; i++) { // remove the last element and add it to the beginning arr.unshift(arr.pop()); } return arr; } let arr = [1, 2, 3, 4, 5]; // rotate an array to the right by 3 positions let rotatedArray = rotateRight(arr, 3); console.log(rotatedArray); // [3, 4, 5, 1, 2] |
2. Using slice() and concat() functions
These functions allow us to create a shallow copy of a part of an array and join two or more arrays together. We can use them to split the array into two parts and swap their positions. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// rotate the array to the left by k positions function rotateLeft(arr, k) { // get the length of the array let n = arr.length; // get the index where to split the array let index = k % n; // slice the array into two parts and concat them in reverse order return arr.slice(index).concat(arr.slice(0, index)); } let arr = [1, 2, 3, 4, 5]; // rotate an array to the left by 3 positions let rotatedArray = rotateLeft(arr, 3); console.log(rotatedArray); // [4, 5, 1, 2, 3] |
3. Using reverse() function
This function reverses the order of the elements in an array. We can use it to reverse the whole array and then reverse each part separately. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// rotate the array to the right by k positions function rotateRight(arr, k) { // get the length of the array let n = arr.length; // get the index where to split the array let index = n - (k % n); // reverse the whole array arr.reverse(); // reverse each part separately arr.reverse(0, index - 1); arr.reverse(index, n - 1); return arr; } let arr = [1, 2, 3, 4, 5]; // rotate an array to the right by 3 positions let rotatedArray = rotateRight(arr, 3); console.log(rotatedArray); // [5, 4, 3, 2, 1] |
That’s all about rotating an array by k positions in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)