How To Rotate An Array To Left In Javascript | letsbug
In this article we are going to rotate an array to left . So basically we are going to shift the elements of the array to the left of the array after some index . And we are going to use javascript for this we you can do this is any language. We some different logic and implementations. Let me explain you what we are going to do here. Suppose we are given an array with these elements arr = [ 1 , 2 , 3 , 4 , 5 ] And we have been given to rotate this array by 2 elements that means that you have to shift all the that are after the index 2 to the left of the array. So you arr will look like this arr = [ 3 , 4 , 5 , 1 , 2 ] So let's start coding this in javascript Array Left Rotation code: "use strict" ; function rotLeft (a, d) { let temp = []; for ( let i = 0 ; i < a .length; i ++ ) { temp . push ( a [( i + d ) % a .length]); } a = temp ; return a ; } let