Subsets II
Subsets II is LeetCode 90 (Medium). Given an array nums that may contain repeated values, return every subset, listing each distinct subset only once. Two subsets are the same when they hold the same values the same number of times, no matter which positions those values came from.
- n is at most 10, so there are at most 1,024 subsets before repeats are removed.
- The subsets may be returned in any order.
- 1 <= nums.length <= 10
- -10 <= nums[i] <= 10
Intuition
Sort the array first, so equal values sit next to each other. Then build each subset by choosing which element comes next, always moving to the right.
At one level of the search, choosing the first 2 or the second 2 as the next element leads to exactly the same subsets, because everything after it can be chosen the same way. So each level tries every distinct value once: it skips an element equal to the one just before it in the same loop.
The skip only works inside one loop. A deeper call starts a fresh loop, so it may still take the second 2 after the first, and [2, 2] is still built.
Listing unique subsets, combinations or permutations from an input with repeats always comes down to this: sort, then never start two sibling branches with the same value. The check is one line inside the usual backtracking loop.
Approach
Before reading on, run the plain Subsets search on [2, 2] and list what it returns. Which two branches produce the same subset, and how could the loop tell them apart? Aim for O(n · 2^n) time.
Sort the array
Sorting puts equal values side by side, so a repeat can be spotted by comparing an element with its left neighbour. It costs O(n log n), which is tiny next to the output.
Save the current subset on every call
dfs(start) first appends a copy of path to res. Each call chooses which element comes next rather than deciding one element, so every call, including the first one with an empty path, stands for a different subset.
Try each later element as the next one
Loop i from start to the end: append nums[i], call dfs(i + 1), then pop it. The child starts at i + 1, so values are always added in sorted order and [1, 2] and [2, 1] cannot both appear.
Skip a value already tried in this loop
If i > start and nums[i] == nums[i - 1], continue to the next i. The equal element just before it was tried as the next pick a moment ago, and its branch already produced every subset this one would.
Code in Python, C++ and Java
Common pitfalls
Skipping with i > 0 instead of i > start
if i > 0 and nums[i] == nums[i - 1]:
continueif i > start and nums[i] == nums[i - 1]:
continuei > 0 also blocks the first pick of a deeper loop, which is a legal second copy. For [2, 2] the result is [[], [2]] and [2, 2] is lost. Only a repeat among siblings of the same loop should be skipped.
Forgetting to sort
res, path = [], [] dfs(0)
nums.sort() res, path = [], [] dfs(0)
The skip compares neighbours, so it only works when equal values are adjacent. For [2, 1, 2] the unsorted run returns [2] twice, and both [2, 1] and [1, 2].
Edge cases
[2, 2, 2] gives [[], [2], [2, 2], [2, 2, 2]]. Each loop takes its first 2 and skips the rest, so there is one subset per count of 2s.
Complexity
path are at most n deep.Subsets II FAQ
Why not generate all subsets with duplicates and put them in a set?
It works if you sort first and store tuples, but it builds every repeated subset just to throw it away. For ten 2s that is 1,024 lists for only 11 distinct answers. The skip rule never builds a repeat.
How many distinct subsets will there be?
Group equal values. A value that appears c times can be used 0 to c times, giving c + 1 choices, and the groups are independent. So the count is the product of (c + 1) over all groups. For [1, 2, 2] that is 2 × 3 = 6.
How is this different from Subsets?
In Subsets every value is different, so every branch gives a new subset and no check is needed. Here the loop form is used because the repeat check is one line inside it.