Last Updated : 22 Jul, 2024
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 maintaining their relative sequence.
There are several methods that can be used to left Rotate by One in an Array in javascript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using the map() methodIn this approach, we are using the map method for Left Rotate by One in an array involves creating a new array where each element is derived from the previous element's position, wrapping around to the start. The first element becomes the last, and the array order is shifted left by one position.
Syntax:
map((element, index, array) => { /* … */ })
Example: In this example, we are using the above-explained approach.
JavaScript
function rotationFunction(arr) {
if (arr.length <= 1) {
return arr;
}
return arr.map((_, index, array) =>
array[(index + 1) % array.length]);
}
let arr1 = [1, 2, 3, 4, 5];
let result = rotationFunction(arr1);
console.log("Original Array:", arr1);
console.log("Array after Left Rotation :", result);
Original Array: [ 1, 2, 3, 4, 5 ] Array after Left Rotation : [ 2, 3, 4, 5, 1 ]Approach 2: Using slice() method
In this approach, we are using the slice method for Left Rotate by One to create a new array by extracting elements after the first, then adding the first element at the end, shifting the array left.
Syntax:
arr.slice(begin, end)
Example: In this example, we are using the above-explained approach.
JavaScript
function rotationalArray(arr) {
if (arr.length <= 1) {
return arr;
}
let element1 = arr[0];
let remainingElement = arr.slice(1);
return [...remainingElements, element1];
}
const arr1 = [10, 20, 30, 40, 50];
const result = rotationalArray(arr1);
console.log("Original Array:", arr1);
console.log("Array after Left Rotate by one :", result);
Output:
Original Array: [ 10, 20, 30, 40, 50 ]Approach 3: Using shift() and push() method
Array after Left Rotate by one : [ 20, 30, 40, 50, 10 ]
In this approach, Using shift() removes the first element, and push() adds it to the end, achieving a left rotation by one position in an array.
Syntax:
if (arr.length <= 1) {
return arr;
}
let elem1 = arr.shift();
arr.push(elem1);
Example: In this example, we are using the above-explained approach.
JavaScript
function ratationalArray(arr) {
if (arr.length <= 1) {
return arr;
}
let elem1 = arr.shift();
arr.push(elem1);
return arr;
}
let arr1 = [15, 16, 17, 18, 19];
let result = ratationalArray([...arr1]);
console.log("Original Array:", arr1);
console.log("Array after Left Rotation:", result);
Original Array: [ 15, 16, 17, 18, 19 ] Array after Left Rotation: [ 16, 17, 18, 19, 15 ]Approach 4: Using for loop
Example:
JavaScript
function leftRotateByOne(arr) {
const temp = arr[0];
for (let i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = temp;
return arr;
}
const arr = [1, 2, 3, 4, 5];
console.log("Rotated Array:", leftRotateByOne(arr));
Rotated Array: [ 2, 3, 4, 5, 1 ]Approach 5: Using destructuring assignment and the spread operator
In this approach, we can use destructuring assignment along with the spread operator to achieve left rotation by one in an array. We first destructure the array into two parts: the first element and the rest of the elements. Then, we use the spread operator to combine the rest of the elements with the first element placed at the end.
Example:
JavaScript
function leftRotateByOne(arr) {
if (arr.length <= 1) {
return arr;
}
const [first, ...rest] = arr;
return [...rest, first];
}
const arr = [1, 2, 3, 4, 5];
console.log("Rotated Array:", leftRotateByOne(arr));
Rotated Array: [ 2, 3, 4, 5, 1 ]Approach 6: Using the reduce() Method
In this approach, we will use the reduce() method to accumulate the elements of the array into a new array that represents the left-rotated version of the original array. The first element will be appended to the end after iterating through the rest of the elements.
Example: In this example, we are using the above-explained approach.
JavaScript
function leftRotateByOneUsingReduce(arr) {
if (arr.length <= 1) {
return arr;
}
return arr.slice(1).reduce((accumulator, currentValue) => {
accumulator.push(currentValue);
return accumulator;
}, [arr[0]]);
}
const arr = [6, 7, 8, 9, 10];
const result = leftRotateByOneUsingReduce(arr);
console.log("Original Array:", arr);
console.log("Array after Left Rotation:", result);
Original Array: [ 6, 7, 8, 9, 10 ] Array after Left Rotation: [ 6, 7, 8, 9, 10 ]Approach 7: Using Array destructuring and Array.prototype.concat()
In this approach, we can use array destructuring along with the concat() method to achieve left rotation by one in an array. Here's how it works.
Example:
JavaScript
function leftRotateByOne(arr) {
if (arr.length <= 1) {
return arr;
}
const [first, ...rest] = arr;
return rest.concat(first);
}
const array = [10, 20, 30, 40, 50];
console.log("Original Array:", array);
const rotatedArray = leftRotateByOne(array);
console.log("Array after Left Rotate by one:", rotatedArray);
Original Array: [ 10, 20, 30, 40, 50 ] Array after Left Rotate by one: [ 20, 30, 40, 50, 10 ]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4