Problem
Section titled “Problem”Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,2]Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]Example 2:
Input: nums = [0]Output: [[],[0]]Constraints:
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Use the same backtracking as Subsets: from position start, choose which element to append next (any nums[i] with i >= start), recurse from i + 1, and record cur at every node.
With duplicates, two different indices holding the same value produce the same subtree. For [1,2,2], choosing “the first 2” or “the second 2” as the next element both give [2], [2,2], … twice. The fix is to sort nums so equal values are adjacent, and then, at one level of the recursion, use each value only once as the next element:
skip
nums[i]wheni > startandnums[i] == nums[i - 1].
Using the second 2 after the first one (deeper in the tree, i == start) is still allowed, so [2,2] is generated — but only once, through the first copy. This removes duplicates at the source instead of filtering them with a set afterwards.
Algorithm
Section titled “Algorithm”- Sort
nums. backtrack(start): append a copy ofcurtoans.- For each
ifromstartton - 1:- if
i > startandnums[i] == nums[i - 1], continue (this value was already tried at this level); - push
nums[i], callbacktrack(i + 1), pop it.
- if
- Call
backtrack(0)and returnans.
Walkthrough
Section titled “Walkthrough”Recursion tree for nums = [1,2,2] (after sorting). The red branch is skipped because the same value 2 was already chosen at that level:
The six green nodes are exactly the six answers.
Complexity
Section titled “Complexity”- Time: — at most subsets are recorded, each copy costs , and every skipped index is charged to the loop of a recorded node. Sorting adds only .
- Space: auxiliary — recursion depth and
cur; the output is .
Edge cases
Section titled “Edge cases”- All elements equal (
[2,2,2]): each level has one usable choice, giving[],[2],[2,2],[2,2,2]. - No duplicates: the skip condition never fires and the result is the ordinary power set.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { vector<vector<int>> ans; vector<int> cur;
void backtrack(const vector<int>& nums, int start) { ans.push_back(cur); for (int i = start; i < (int)nums.size(); i++) { if (i > start && nums[i] == nums[i - 1]) continue; // value already used at this level cur.push_back(nums[i]); backtrack(nums, i + 1); cur.pop_back(); } }
public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { sort(nums.begin(), nums.end()); backtrack(nums, 0); return ans; }};class Solution { private final List<List<Integer>> ans = new ArrayList<>(); private final List<Integer> cur = new ArrayList<>();
private void backtrack(int[] nums, int start) { ans.add(new ArrayList<>(cur)); for (int i = start; i < nums.length; i++) { if (i > start && nums[i] == nums[i - 1]) continue; // value already used at this level cur.add(nums[i]); backtrack(nums, i + 1); cur.remove(cur.size() - 1); } }
public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); backtrack(nums, 0); return ans; }}class Solution: def subsetsWithDup(self, nums: list[int]) -> list[list[int]]: nums.sort() ans, cur = [], []
def backtrack(start): ans.append(cur[:]) for i in range(start, len(nums)): if i > start and nums[i] == nums[i - 1]: continue # value already used at this level cur.append(nums[i]) backtrack(i + 1) cur.pop()
backtrack(0) return ansGive it a try first
Attempt the problem on LeetCode — the code will still be here.
![Recursion tree for nums = [1,2,2]: six kept subsets [ ], [1], [2], [1,2], [2,2] and [1,2,2]; the second copy of 2 is skipped at the root and under [1], avoiding duplicate [2] and [1,2]](/CodePrime/img/codes/subsets-ii/recursion-tree.png)