Skip to content
Veltoryx Technologies
FreeProblem 75● MediumLeetCode #90 ↗4 min readUpdated 30 October 2026

Subsets II

Generate all unique subsets from an array containing duplicate integers using sorting and sibling-level duplicate pruning.

01The 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.

InputOutputWhy
nums = [1, 2, 2][[], [1], [1, 2], [1, 2, 2], [2], [2, 2]][1, 2] using first 2 is identical to [1, 2] using second 2; duplicates omitted
nums = [0][[], [0]]Single element

Constraints: 1 <= nums.length <= 10, -10 <= nums[i] <= 10.

02How to spot it

  • Similar to Subsets (#74), but nums contains duplicate values.
  • If we do not prune, identical subsets like [1, 2] will be produced multiple times.
  • Sorting nums groups identical values side-by-side.
  • If nums[i] == nums[i - 1] and i > start, nums[i] is a duplicate choice at the same recursion level (sibling branch), so choosing it would mirror the previous branch.

So: sort + skip duplicate siblings (i > start && nums[i] == nums[i-1]).

03Pseudocode

text
sort nums
build(start, cur):
  record a copy of cur
  for i from start to n - 1:
    if i > start and nums[i] == nums[i-1]:
      continue
    choose, build(i + 1), un-choose
  1. Sort nums so duplicates are adjacent.
  2. Record cur.
  3. If i > start and nums[i] == nums[i - 1], skip this choice.
  4. Recurse with i + 1, then backtrack.

04Watch it work

Visualizer · step through the algorithmnums = [1,2,2]

sorted nums

10
21
22
i

current subset

status

sorted

Sort array [1, 2, 2] to place identical values adjacent for duplicate pruning.

Pseudocode

  1. sort nums
  2. build(start, cur):
  3. record a copy of cur
  4. for i from start to n - 1:
  5. if i > start and nums[i] == nums[i-1]:
  6. continue
  7. choose, build(i + 1), un-choose

Variables

sorted
1,2,2
step 1 / 19

05Step-by-step dry run

  1. 1

    sorted nums

    10
    21
    22
    i

    current subset

    status

    sorted

    Sort array [1, 2, 2] to place identical values adjacent for duplicate pruning.

    sorted = 1,2,2

  2. 2

    sorted nums

    10
    21
    22
    i

    current subset

    status

    recorded []

    Record subset: [].

    subset = total = 1

  3. 3

    sorted nums

    10
    21
    22
    i

    current subset

    10

    status

    choose 1

    Choose sorted[0] = 1. Current: [1]. Recurse with start = 1.

    i = 0val = 1

  4. 4

    sorted nums

    10
    21
    22
    i

    current subset

    10

    status

    recorded [1]

    Record subset: [1].

    subset = 1total = 2

  5. 5

    sorted nums

    10
    21
    22
    i

    current subset

    10
    21

    status

    choose 2

    Choose sorted[1] = 2. Current: [1, 2]. Recurse with start = 2.

    i = 1val = 2

  6. 6

    sorted nums

    10
    21
    22
    i

    current subset

    10
    21

    status

    recorded [1, 2]

    Record subset: [1, 2].

    subset = 1,2total = 3

  7. 7

    sorted nums

    10
    21
    22
    i

    current subset

    10
    21
    22

    status

    choose 2

    Choose sorted[2] = 2. Current: [1, 2, 2]. Recurse with start = 3.

    i = 2val = 2

  8. 8

    sorted nums

    10
    21
    22

    current subset

    10
    21
    22

    status

    recorded [1, 2, 2]

    Record subset: [1, 2, 2].

    subset = 1,2,2total = 4

  9. 9

    sorted nums

    10
    21
    22
    i

    current subset

    10
    21

    status

    un-choose 2

    Backtrack: un-choose 2. Restore to [1, 2].

    popped = 2

  10. 10

    sorted nums

    10
    21
    22
    i

    current subset

    10

    status

    un-choose 2

    Backtrack: un-choose 2. Restore to [1].

    popped = 2

  11. 11

    sorted nums

    10
    21
    22
    i

    current subset

    10

    status

    skip duplicate 2

    Duplicate at index 2 (value 2 == 2) on same recursion level! Skip to prevent duplicate subsets.

    i = 2val = 2

  12. 12

    sorted nums

    10
    21
    22
    i

    current subset

    status

    un-choose 1

    Backtrack: un-choose 1. Restore to [].

    popped = 1

  13. 13

    sorted nums

    10
    21
    22
    i

    current subset

    20

    status

    choose 2

    Choose sorted[1] = 2. Current: [2]. Recurse with start = 2.

    i = 1val = 2

  14. 14

    sorted nums

    10
    21
    22
    i

    current subset

    20

    status

    recorded [2]

    Record subset: [2].

    subset = 2total = 5

  15. 15

    sorted nums

    10
    21
    22
    i

    current subset

    20
    21

    status

    choose 2

    Choose sorted[2] = 2. Current: [2, 2]. Recurse with start = 3.

    i = 2val = 2

  16. 16

    sorted nums

    10
    21
    22

    current subset

    20
    21

    status

    recorded [2, 2]

    Record subset: [2, 2].

    subset = 2,2total = 6

  17. 17

    sorted nums

    10
    21
    22
    i

    current subset

    20

    status

    un-choose 2

    Backtrack: un-choose 2. Restore to [2].

    popped = 2

  18. 18

    sorted nums

    10
    21
    22
    i

    current subset

    status

    un-choose 2

    Backtrack: un-choose 2. Restore to [].

    popped = 2

  19. 19

    sorted nums

    10
    21
    22
    i

    current subset

    status

    skip duplicate 2

    Duplicate at index 2 (value 2 == 2) on same recursion level! Skip to prevent duplicate subsets.

    i = 2val = 2

06Java solution

SubsetsWithDup.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SubsetsWithDup {
    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        build(nums, 0, new ArrayList<>(), res);
        return res;
    }

    private static void build(int[] nums, int start, List<Integer> cur, List<List<Integer>> res) {
        res.add(new ArrayList<>(cur));
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i - 1]) continue;
            cur.add(nums[i]);
            build(nums, i + 1, cur, res);
            cur.remove(cur.size() - 1);
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 2};
        System.out.println(subsetsWithDup(nums));
    }
}
Expected output
[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]

07Why it works

The condition i > start is the cornerstone of this algorithm:

  • When i == start, nums[i] is the first element considered at this level. If it equals a duplicate in a deeper level, that's fine (e.g. [2, 2] is allowed).
  • When i > start, we are considering an alternative candidate at the same branch level. If nums[i] == nums[i - 1], exploring nums[i] would create the exact same family of subsets that nums[i - 1] already generated. Skipping it prunes the duplicate branch entirely.

08Time and space

  • Time complexity: O(n * 2^n). Sorting takes O(n log n). In the worst case with all distinct elements, it generates 2^n subsets.
  • Space complexity: O(n) stack and temporary list memory.

09Edge cases

  • All identical elements (e.g. [2, 2, 2]): Produces exactly [[], [2], [2, 2], [2, 2, 2]].
  • Negative duplicates (e.g. [-1, -1]): Handled identically after sorting.

10Common mistakes

  • Forgetting i > start and writing i > 0 && nums[i] == nums[i - 1]. This wrongly prevents selecting duplicates down the recursive chain, making it impossible to produce [2, 2].
  • Forgetting to sort. If nums = [2, 1, 2], unsorted duplicates cannot be detected by comparing nums[i] == nums[i - 1].

11Other approaches

ApproachTimeSpaceNotes
Set of listsO(n * 2^n)O(n * 2^n)Inefficient memory hashing
Sort + sibling pruningO(n * 2^n)O(n) stackOptimal, zero duplicate generation

12In the interview

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why is i > start necessary in addition to nums[i] == nums[i - 1]?

  2. 2. What happens if we forget to sort nums first?

14Solve it yourself

Try tracing nums = [4, 4, 4, 1, 4]:

  1. What does the array look like after sorting?
  2. How many total unique subsets are produced?