Subsets II
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.
nums, an array of integers. Values can repeat. It has between 1 and 10 numbers.
A list of all distinct subsets. Each subset is a list of numbers, and no subset appears twice.
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.
Two subsets: the empty one, and the one that holds 0.
- • 1 ≤ nums.length ≤ 10
- • −10 ≤ nums[i] ≤ 10
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every yes/no pattern, sort each subset, and let a set drop the repeats
Sort, then in each loop skip nums[i] when it equals nums[i − 1] and i > start
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.