"Subsets II" is the natural follow-up to the Subsets problem. The key difference is that the input array can now contain duplicate elements. Your task is still to return all possible subsets, but the result must not contain any duplicate subsets. For example, if the input is [1, 2, 2], the subsets [1, 2] and [1, 2] (using the different 2s) should only appear once in your final list.
Companies like Meta, Amazon, and TikTok ask this to see if you can handle "Duplicate Removal" in a recursive context. It's one thing to generate all combinations; it's another to do so efficiently without generating and then filtering out duplicates. It tests if a candidate knows the trick of sorting the input and using a simple condition to skip duplicate elements during recursion.
The pattern used is Backtracking with Pruning.
i and arr[i] == arr[i-1], and you have already skipped arr[i-1], you should also skip arr[i].
This simple "skip" condition ensures that for any group of duplicate numbers, you only generate subsets that include them in a consistent way (e.g., always use the first occurrence before the second), thus preventing duplicate subsets from being formed.Array: [2, 1, 2].
The most frequent mistake is forgetting to sort the array first. Without sorting, the "skip duplicate" logic won't work because duplicates won't be adjacent. Another mistake is using a Hash Set to store subsets to remove duplicates at the end. While this works, it is less efficient than pruning the recursion tree and is often frowned upon in high-level interviews as it shows a lack of control over the generation process.
When preparing for the Subsets II interview question, focus on the "Skip Condition": if (i > start && nums[i] == nums[i-1]) continue;. Practice explaining why this works—it's about ensuring that we don't start a new branch with a number we've already tried at the same level of the recursion tree.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Subsets | Medium | Solve | |
| Count Number of Maximum Bitwise-OR Subsets | Medium | Solve | |
| Maximum Length of a Concatenated String with Unique Characters | Medium | Solve | |
| Non-decreasing Subsequences | Medium | Solve | |
| Maximum Points in an Archery Competition | Medium | Solve |