Facebook Pixel

shuffle()

JavaScript
easy

Write a function shuffle that accepts an array and returns a new array with the elements in randomized order. Ensure that all elements from the original array are present exactly once and that the original array is not mutated.

Example Inputs & Outputs

Input: [1, 2, 3, 4, 5] Possible Output: [3, 1, 5, 2, 4] Input: ['a', 'b', 'c'] Possible Output: ['b', 'c', 'a']

Constraints & Edge Cases

  • The function should return a new array (do not mutate the original).
  • All original elements must be present exactly once.
  • An empty array should return an empty array.
  • Input array may contain duplicates (handle gracefully).
  • The output should be randomized on each function call.

Solve Similar questions 🔥