HackerRank Left Rotation | JS Solution
1. Problem
Given an array of numbers we need to rotate the numbers to the left n
times. First number becomes last creating a circular array.
2. Test
let testArr = [3, 4, 5, 1, 2];
let testN = 3;
3. Data Types & Structures
Array and number
4. Code
function rotLeft(arr, n) {
let counter = 0;
let rotatedElement;
while (counter < n) {
rotatedElement = arr.shift();
arr.push(rotatedElement);
counter++;
}
return arr;
}
Source:
Left Rotation Algorithm by HackerRank