Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program for Left Rotate by One in an Array

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:

  • Using the map() method
  • Using slice() method
  • Using shift() and push() method
  • Using for loop

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.

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


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.

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

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


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:

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


Output

Rotated Array: [ 2, 3, 4, 5, 1 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments