Last active
June 16, 2021 15:38
-
-
Save clarketm/21746e1505aa48b60f7116f2092582f6 to your computer and use it in GitHub Desktop.
Cyclic Rotation (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cyclicRotation(array, times) { | |
var rotatedArray = array; | |
while (times > 0) { | |
var currentArray = rotatedArray.slice(); | |
for (var i = 0; i < currentArray.length; i++) { | |
rotatedArray[(i+1)%currentArray.length] = currentArray[i]; | |
} | |
times--; | |
} | |
return rotatedArray; | |
} | |
console.log(cyclicRotation([], 10)); // [] | |
console.log(cyclicRotation([2,5], 2)); // [2, 5] | |
console.log(cyclicRotation([2,5,5,5,5,5,5,3,3,2,1,00,6,4], 33)); // [2,1,00,6,4,2,5,5,5,5,5,5,3,3] | |
console.log(cyclicRotation([1,2,3,4,5,6,7,8,9], 8)); // [2,3,4,5,6,7,8,9, 1] |
bumsyalao
commented
Jul 11, 2019
function solution(A, K) { if (!!A.length) { while (K!=0) { K--; A = [A.pop(), ...A]; } return A; } return []; }
function solution(A) {
let arr =A;
let dict ={};
let hasIt = arr.filter(function(item,i){
if(!dict[item]){
dict[item] =0
}
dict[item] +=1
return arr[i]==arr[i+2]
})
for(let [key,val] of Object.entries(dict)){
if(val ==1){
return parseInt(key,10)
}
}
}
Here's my version
function solution(A, K) {
if (A.length === 0 || K === 0 || K % A.length === 0) return A;
for (let i = 0; i < K; i++) {
let a = A.pop();
A.unshift(a);
}
return A; }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment