#90

Subsets II

Medium Backtracking with duplicate skip

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return all distinct subsets of an array that may contain duplicates.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: subsets without duplicates when the input has repeats.

Why this works: sort, then at the same level skip a value equal to the one just tried.

Example: [1, 2, 2]

[], [1], [1,2], [1,2,2], [2], [2,2]the second 2 is skipped as a starting choice at the same level

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Skipping with i > 0 instead of i > start, which also removes valid subsets like [2, 2].

Solve on LeetCode ↗
Complexity
  • Time: O(n · 2^n).
  • Space: O(n).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon