Skip to content

Instantly share code, notes, and snippets.

@wataruoguchi
Created July 30, 2019 03:04
Show Gist options
  • Save wataruoguchi/e6c454a20956c749aaa159be722036e3 to your computer and use it in GitHub Desktop.
Save wataruoguchi/e6c454a20956c749aaa159be722036e3 to your computer and use it in GitHub Desktop.
Program for array rotation
// Program for array rotation
// https://www.geeksforgeeks.org/array-rotation/
function rotate(arr, d, n) {
// Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.
const stash = arr.splice(0, d);
return arr.concat(stash);
}
const arr = [1,2,3,4,5,6,7];
const res = rotate(arr, 2, arr.length);
console.log(res.join(',') === '3,4,5,6,7,1,2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment