Toolbox-XToolbox-X

Rotate Array

Rotates an array to the left or right by a specified number of steps.

rotateArray

The rotateArray function rotates an array to the left or right by a specified number of steps. Positive values rotate to the right, while negative values rotate to the left.

Function Signature

function rotateArray<T>(arr: T[], steps: number): T[]

Parameters

  • arr (T[]): The array to rotate.
  • steps (number): Number of positions to rotate.
    • Positive: rotates to the right.
    • Negative: rotates to the left.

Returns

  • T[]: A new array with elements rotated as specified.

Example

playground.ts

Notes

  • This function ensures consistent behavior by normalizing the number of steps, even if it is greater than the array length or negative.
  • Does not mutate the original array.

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page