Jaspreet singhleetcode.com Problem...
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution must not contain duplicate subsets.
Generate all possible subsets using the classic Pick / Not Pick recursion.
After generating every subset, store them in a Set to remove duplicates.
While this works, many duplicate subsets are generated unnecessarily and later filtered out.
Set<List<Integer>> set = new HashSet<>();
generateAllSubsets();
return new ArrayList<>(set);
Notice:
nums = [1,2,2]
After sorting:
[1,2,2]
If we start a subset with the first 2, we should not start another identical subset with the second 2 at the same recursion level.
Instead of generating duplicates and removing them later, we can simply skip duplicate choices while building subsets.
Whenever you see:
Think:
Backtracking + Sorting + Duplicate Skipping
After sorting:
1 2 2
At the same recursion level:
if(i != ind && nums[i] == nums[i - 1])
continue;
This ensures we only consider the first occurrence and skip duplicate starts.
import java.util.*;
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
helper(0, nums, new ArrayList<>(), ans);
return ans;
}
private void helper(int ind,
int[] nums,
List<Integer> ds,
List<List<Integer>> ans) {
ans.add(new ArrayList<>(ds));
for (int i = ind; i < nums.length; i++) {
if (i != ind && nums[i] == nums[i - 1])
continue;
ds.add(nums[i]);
helper(i + 1, nums, ds, ans);
ds.remove(ds.size() - 1);
}
}
}
nums = [1,2,2]
[1,2,2]
[]
├── [1]
│ ├── [1,2]
│ │ └── [1,2,2]
│
├── [2]
│ └── [2,2]
│
└── Skip second 2
[]
[1]
[1,2]
[1,2,2]
[2]
[2,2]
Sorting places duplicates together:
2 2
Now we can easily identify repeated choices and skip them.
Without sorting, duplicate detection becomes much harder.
if(i != ind && nums[i] == nums[i - 1])
continue;
Meaning:
If the current number is the same as the previous number and both belong to the same recursion level, skip it.
This prevents duplicate subsets from being generated.
| Metric | Complexity |
|---|---|
| Time Complexity | O(2ᴺ × N) |
| Space Complexity | O(N) |
Reason:
2ᴺ subsets.O(N).N.Sort the array and use backtracking. While generating subsets, skip duplicate elements at the same recursion level to avoid duplicate subsets.
Generate All Subsets
+
Duplicates Present
+
Unique Answers Needed
=> Sort First
=> Backtracking
=> Skip Duplicates