JavaScript Program for array rotation
Last Updated :
28 Feb, 2024
Array rotation in JavaScript involves shifting elements within an array to the left or right by a specified number of positions.
There are several approaches to rotate an array in JavaScript which are as follows:
Using Array Slice and Concatenation Method
This approach involves slicing the array into two parts based on the rotation index and then concatenating them in the desired order. This method first calculates the rotation index, then slices the array accordingly, and concatenates the parts in the rotated order.
Syntax:
// Create the rotated array by concatenating two slices
const rotatedArray = arr.slice(rotateBy).concat(arr.slice(0, rotateBy));
// Return the rotated array
return rotatedArray;
Example: TypeScript Array Rotation: Left Rotate by 2 Positions - Demonstrating a function to rotate an array leftward by a specified number of positions.
JavaScript
function rotateArray(arr, rotateBy) {
const n = arr.length;
rotateBy %= n;
return arr.slice(rotateBy).concat(arr.slice(0, rotateBy));
}
const originalArray = [1, 2, 3, 4, 5];
const rotatedArray = rotateArray(originalArray, 2);
console.log(rotatedArray);
Using the Array Splice and Push method
This approach involves using the splice() method to remove elements from the beginning of the array and then push them to the end. Here, we first remove the elements to be rotated from the beginning of the array using splice() and then push them to the end using the spread operator (...).
Example: TypeScript In-Place Array Rotation: Left Rotate by 2 Positions - Demonstrating a function for in-place left rotation of an array by a specified number of positions.
JavaScript
function rotateArray(arr, rotateBy) {
const n = arr.length;
rotateBy %= n;
arr.push(...arr.splice(0, rotateBy));
return arr;
}
const originalArray = [1, 2, 3, 4, 5];
const rotatedArray = rotateArray(originalArray, 2);
console.log(rotatedArray);
Using Array Reversal
This approach involves three steps first reversing the first part of the array, reversing the second part, and then reversing the whole array. This method needs a helper function reverse() to reverse elements within specified ranges and allow us to perform array rotation efficiently.
Example: TypeScript Array Rotation: Left Rotate by 2 Positions - Utilizing Reversal Algorithm - Demonstrating an efficient in-place left rotation algorithm for arrays using reversal.
JavaScript
function rotateArray(arr, rotateBy) {
const n = arr.length;
rotateBy %= n;
reverse(arr, 0, rotateBy - 1);
reverse(arr, rotateBy, n - 1);
reverse(arr, 0, n - 1);
return arr;
}
function reverse(arr, start, end) {
while (start < end) {
const temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
const originalArray = [1, 2, 3, 4, 5];
const rotatedArray = rotateArray(originalArray, 2);
console.log(rotatedArray);
Similar Reads
JavaScript Program for Left Rotate by One in an Array In this article, we are going to learn about left Rotation by One in an Array by using JavaScript, Left rotation by one in an array means shifting all elements from one position to the left, with the last element moving to the first position. The order of elements is cyclically rotated while maintai
5 min read
Javascript Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read
JavaScript Program to Find Circular Rotation of an Array by K Positions Given an array of integers and an integer K, rotate the array to the right by K positions in a circular manner. Examples: Input: nums = [1, 2, 3, 4, 5], k=2Output: [4, 5, 1, 2, 3]Input: nums = [6, 7, 8, 9, 10], k=1Output: [10, 9, 6, 7, 8]Below are the approaches to finding the Circular rotation of a
2 min read
Rotate Image by 90 Degree using JavaScript In this article, we are going to learn how we can rotate images by 90 degrees clockwise using JavaScript. In simple words, we are given a matrix, and we have to rotate it by 90 degrees clockwise. To understand the question clearly, below is the image illustration explaining how the matrix looks when
3 min read
Javascript Program for Reversal algorithm for array rotation Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example : Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output : arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Recommended: Please solve it on âPRACTICE â first, before moving on to the so
3 min read
Javascript Program for Block swap algorithm for array rotation Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br i
3 min read