Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 438d991

Browse files
authored
Merge pull request vJechsmayr#173 from saichandernagamalla/Permutations2
vJechsmayr#157 Added code for Permutations2
2 parents af0b0bb + 441c989 commit 438d991

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var permuteUnique = function(nums) {
6+
var uniqueList = [nums];
7+
performAllPermutations(uniqueList, nums, nums.length);
8+
return uniqueList;
9+
};
10+
11+
function performAllPermutations(uniqueList, nums, length){
12+
for(var i = 0; i < length; i++){
13+
if(i == length - 1){
14+
return;
15+
}
16+
17+
var newList = nums.slice();
18+
newList[i] = nums[i+1];
19+
newList[i+1] = nums[i];
20+
21+
//check if newList is present in uniqueList
22+
var doCombExists = false;
23+
for(var j = 0; j < uniqueList.length; j++){
24+
var k = 0;
25+
for(k = 0; k < length; k++){
26+
if(newList[k] !== uniqueList[j][k]){
27+
break;
28+
}
29+
}
30+
if(k == length){
31+
doCombExists = true;
32+
break;
33+
}
34+
}
35+
if(!doCombExists){
36+
uniqueList.push(newList);
37+
performAllPermutations(uniqueList,newList, length);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)