Back to all solutions

#189 - Rotate Array

Problem Description

Given an array, rotate the array to the right by k steps, where k is non-negative.

Solution

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function(nums, k) {
  nums.unshift(...nums.splice((k % nums.length) * -1));
};