Skip to content

Commit 51a7586

Browse files
authored
Merge pull request vJechsmayr#163 from karansinghgit/master
Create 47_Permutations2.js, closes vJechsmayr#157
2 parents 06f311d + d6adab8 commit 51a7586

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var permuteUnique = function(nums) {
2+
3+
let permutations = []
4+
5+
const findUniquePerm = function(visited = new Set(), currPerm = []) {
6+
if (currPerm.length === nums.length){
7+
permutations.push(currPerm)
8+
return
9+
}
10+
let uniqueVal = new Set()
11+
12+
for (let i = 0; i < nums.length; i++) {
13+
if (!uniqueVal.has(nums[i]) && !visited.has(i)) {
14+
uniqueVal.add(nums[i])
15+
findUniquePerm(new Set([...visited, i]), [...currPerm, nums[i]])
16+
}
17+
}
18+
}
19+
20+
findUniquePerm()
21+
22+
return permutations
23+
};

0 commit comments

Comments
 (0)