Permutations II
Description
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ]
Solution(javascript)
/**
* @param {number[]} nums
* @return {number[][]}
*/
const permuteUnique = (nums) => {
const result = []
const aux = (list = [], current = []) => {
if (list.length === 0) {
result.push(current)
}
const visited = {}
for (let i = 0; i < list.length; i++) {
if (!visited[list[i]]) {
visited[list[i]] = true
aux(list.filter((v, index) => index !== i), [...current, list[i]])
}
}
}
aux(nums)
return result
}