Open In App

JavaScript Program for Left Rotate by One in an Array

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
4 Likes
Like
Report

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.


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.

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.


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

Approach 4: Using for loop

  • In this approach, we will store the first element in the temp variable.
  • Now, using the for loop to iterate the array and moving elements one position to the left
  • After that place the stored first element at the end.
  • The time complexity is O(N) and space complexity is O(1)

Example:


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


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


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


Output
Original Array: [ 10, 20, 30, 40, 50 ]
Array after Left Rotate by one: [ 20, 30, 40, 50, 10 ]

Next Article

Similar Reads