← Back to challenges

Array's Permutations

JavaScriptHardarraysrecursionfunctional_programming

Instructions

Create a function that takes an array of numbers and returns an array containing all possible permutations.

Examples

permutations([1]) ➞ [[1]]

permutations([1, 7]) ➞ [[1, 7], [7, 1]]

permutations([2, 4, 7]) ➞ [[2, 4, 7], [2, 7, 4], [4, 2, 7], [4, 7, 2], [7, 2, 4], [7, 4, 2]]

Notes

All given arrays do not contain duplicates.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.