shuffle()
JavaScript
easy
10 mins
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 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
