1
- // const isEqual = _.isEqual;
2
-
3
1
import { uniqBy } from "../deps.ts" ;
4
2
5
- // const uniqBy = _.uniqBy;
6
3
export default function permuteUnique ( nums : number [ ] ) : number [ ] [ ] {
7
4
if ( nums . length == 0 ) {
8
5
return [ ] ;
@@ -11,19 +8,10 @@ export default function permuteUnique(nums: number[]): number[][] {
11
8
return [ nums ] ;
12
9
}
13
10
14
- // const visited = new Set<string>();
15
11
const result : number [ ] [ ] = [ ] ;
16
12
17
13
permute_index ( nums , ( numsres : number [ ] ) => {
18
- // const numsres = indexs.map((v) => nums[v]);
19
- // const numsres = indexs;
20
- // const key = numsres.join(",");
21
- // if (visited.has(key)) {
22
- // return;
23
- // } else {
24
- // visited.add(key);
25
14
result . push ( numsres ) ;
26
- // }
27
15
} ) ;
28
16
return uniqBy ( result , ( a : number [ ] ) => Array . prototype . join . call ( a , "," ) ) ;
29
17
}
@@ -37,39 +25,21 @@ function permute_index(nums: number[], output: (indexs: number[]) => void) {
37
25
output ( [ 0 ] . map ( ( v ) => nums [ v ] ) ) ;
38
26
return ;
39
27
}
40
- // if (k === 2) {
41
- // // output([0, 1].map((v) => nums[v]));
42
- // // output([1, 0].map((v) => nums[v]));
43
- // const arrays: number[][] = uniqBy(
44
- // [
45
- // [1, 0],
46
- // [0, 1],
47
- // ].map((a) => a.map((v) => nums[v])),
48
- // isEqual
49
- // );
50
- // console.log(arrays)
51
- // for (const res of arrays) {
52
- // output(res);
53
- // }
54
- // return;
55
- // }
56
28
57
29
permute_index ( nums . slice ( 0 , - 1 ) , ( indexs ) => {
58
30
const arrays : number [ ] [ ] = [ ] ;
59
31
for ( let i = 0 ; i < indexs . length + 1 ; i ++ ) {
60
32
const b = Array . from ( indexs ) ;
61
33
b . splice ( i , 0 , nums . slice ( - 1 ) [ 0 ] ) ;
62
34
arrays . push ( b ) ;
63
- // output(b);
64
35
}
65
- // console.log(arrays);
36
+
66
37
for (
67
38
const res of uniqBy (
68
39
arrays ,
69
40
( a : number [ ] ) => Array . prototype . join . call ( a , "," ) ,
70
41
)
71
42
) {
72
- // console.log(res);
73
43
output ( res ) ;
74
44
}
75
45
} ) ;
0 commit comments