-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubsets II.js
37 lines (29 loc) · 863 Bytes
/
Subsets II.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
// Note: The solution set must not contain duplicate subsets.
// Example:
// Input: [1,2,2]
// Output:
// [
// [2],
// [1],
// [1,2,2],
// [2,2],
// [1,2],
// []
// ]
// This is much similar to subset 1 with 2 exceptions
var subsetsWithDup = function(nums) {
let result = [];
// we need to sort the items(UNIQUE to subset 2)
nums = nums.sort((a, b) => a - b);
function backtrack(current, start) {
result.push(current);
for (let i = start; i < nums.length; i++) {
// we need to skip (UNIQUE to subset 2)
if (i > start && nums[i] === nums[i - 1]) continue;
backtrack([...current, nums[i]], i + 1);
}
}
backtrack([], 0);
return result;
};