Subsets II
Problem
Given an integer array nums that may contain repeated values, return every distinct subset of it. Two subsets are the same if they contain the same values with the same multiplicities, regardless of which original positions were chosen. Output order does not matter.
- Up to 12 elements, which may repeat.
- Each value is between -15 and 15.
Examples
nums = [3, 5, 5]→[[], [3], [5], [3,5], [5,5], [3,5,5]]. Choosing the first 5 alone or the second 5 alone gives the same subset, counted once.nums = [7]→[[], [7]].nums = [9, 9, 9]→[[], [9], [9,9], [9,9,9]]. Only the count of 9s matters.
Hints
- Run the ordinary subsets backtracking on
[1, 2, 2]and write down which paths produce the same subset. What do those paths have in common? - If equal values sit next to each other, then at any given depth choosing the second copy after skipping the first can only recreate a branch you already explored.
- Sort, then in the loop skip
nums[i]wheni > startandnums[i] == nums[i-1].
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.