Permutations
Description
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
Solution(javascript)
const permute = (nums = []) => {
const result = []
const aux = (list = [], current = []) => {
if (list.length === 0) {
result.push(current)
}
list.forEach((number, index) => {
aux(list.filter((v, index2) => index2 !== index), [...current, number])
})
}
aux(nums)
return result
}