← Back to DSA Animator
Subsets II LC #90 Medium Backtracking · Skip Duplicates
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]]
Explanation: Sort first, then skip duplicate choices at the same recursion depth.
Example 2
Input: nums = [0]
Output: [[],[0]]
Constraints: 1 ≤ nums.length ≤ 10  |  −10 ≤ nums[i] ≤ 10
Try Examples
Custom:
Approach
Sort + Skip Duplicate Choices
Sort first (duplicates become adjacent). Skip: if i>start AND nums[i]==nums[i-1], the same value was already tried at this level — skip to avoid duplicate subsets.
Input Array (sorted)
Current Subset
curr = [ ] (empty)
Results 0 collected
Variables
curr
[ ]
start
0
total found
0
Step Logic
Press ▶ Play or Next Step to begin.
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Sort first so duplicates are adjacent
2
Add curr to results at every call (including empty)
3
Skip: if i>start AND nums[i]==nums[i-1] → continue (same value already tried at this level)
4
i>start check: allows first occurrence, skips repeats at same depth
Time
O(n·2^n)
Space
O(n)
Why Backtracking Works

Sort brings duplicates together. The skip condition i>start && nums[i]==nums[i-1] prevents choosing the same value twice at the same recursion depth, avoiding duplicate subsets while still allowing the same value at different depths (e.g., [2] and [2,2] are both valid when there are two 2s).