2Brute2Best
🔥0

Subsets II

Medium·LeetCode #90 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given an array nums that can hold the same value more than once. Return every distinct subset of nums, including the empty subset.

Two subsets count as the same when they hold the same values. So [2] must appear only once in your answer, even when nums has two 2s. The order of the subsets in your answer does not matter.

Input

nums, an array of integers. Values can repeat. It has between 1 and 10 numbers.

Output

A list of all distinct subsets. Each subset is a list of numbers, and no subset appears twice.

Example 1
Input: nums = [1, 2, 2]
Output: [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]

Picking by position gives 2³ = 8 subsets, but the two 2s make some of them look the same. For example, [2] can be made from either 2. That leaves 6 distinct subsets.

Example 2
Input: nums = [0]
Output: [[], [0]]

Two subsets: the empty one, and the one that holds 0.

Constraints
  • • 1 ≤ nums.length ≤ 10
  • • −10 ≤ nums[i] ≤ 10
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“may contain duplicates”
→ Equal values make the same subset from different positions. The search must build each distinct subset once, not once per position.
“return all possible subsets”
→ The same template as Subsets: each call records its path, and each pick comes from a later index.
“distinct subsets”
→ Two subsets are the same when their values match, so the test compares values, not positions.
03

Approach Complexity Ladder

Brute Force — every subset, sorted, kept in a setO(N² · 2^N)O(N · 2^N)

Try every yes/no pattern, sort each subset, and let a set drop the repeats

Optimal — sort, then skip repeats inside each loopO(N · 2^N)O(N)

Sort, then in each loop skip nums[i] when it equals nums[i − 1] and i > start

Brute Force Intuition

Use the same yes/no loop as in Subsets. Sort each subset, so that [2, 1] and [1, 2] look the same, and insert it into a set. A set keeps one copy of each distinct value, so its contents are the answer.

Recognition rule for this sub-pattern:

Every call is a subset; add only elements to the right of the last one (a start index). Sort first and skip equal neighbours at the same depth to avoid duplicate subsets.
Remember this · Backtracking
Trigger
"All subsets / permutations / combinations", place items under rules (N-Queens, Sudoku), find a path in a grid.
Think
At each step, what are my choices, and how do I undo a choice?
Key question
What is my state, when is it complete, and when do I prune?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.