Back to Practice
#0246
Subsets With Duplicates
MediumDSA28 min20 XP
Problem
Return all unique subsets of nums. Each subset and the final output should be sorted lexicographically.
Why This Matters
This problem teaches the backtracking duplicate-skip rule that also appears in permutations and combination-sum variants.
Function Signature
def subsets_with_dup(nums):
Examples
Example 1
Inputnums = [1, 2, 2]
Output[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
The second 2 is not allowed to start the same branch twice.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
One duplicate
Input[1, 2, 2]
Expected[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
All unique subsets are returned once.
All same
Input[2, 2]
Expected[[], [2], [2, 2]]
Only three unique subsets exist.
Hidden Test Categories
empty inputthree duplicatesunsorted input