Open In App

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 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() method

In 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.




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);

Output
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.




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 ]
Array after Left Rotate by one : [ 20, 30, 40, 50, 10 ]

Approach 3: Using shift() and push() method

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.




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);

Output
Original Array: [ 15, 16, 17, 18, 19 ]
Array after Left Rotation: [ 16, 17, 18, 19, 15 ]

Approach 4: Using for loop

Example:




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));

Output
Rotated Array: [ 2, 3, 4, 5, 1 ]


Article Tags :