AceYourInterviews
Checking sign-in…

119. Subsets II — Duplicates

IntermediateSorted sibling pruning for unique value subsets

List the different multisets you can choose from the input occurrences. Each occurrence can be used at most once, so a repeated value may be chosen as many times as it appears.

Different position selections that yield the same value subset must not create duplicate output rows. Preserve the contract's ordering and ownership rules.

Example 1

Input

[2,1,2]

Output

[[],[1],[1,2],[1,2,2],[2],[2,2]]

Explanation: Sorting a clone makes equal choices adjacent; two copies of 2 may be chosen on successive depths.

Example 2

Input

[]

Output

[[]]

Explanation: The root path is a valid subset and must be emitted.

Example 3

Input

[7,7,7]

Output

[[],[7],[7,7],[7,7,7]]

Explanation: Four multiplicity choices exist, rather than eight distinct value subsets.

Constraints

  • 0 <= nums.length <= 16
  • -231 <= nums[i] <= 231 - 1
Hints
Hint 1

Keep the increasing-index frontier from ordinary subsets.

Hint 2

Sort a clone so equal sibling choices become adjacent.

Hint 3

Emit a snapshot at entry, skip i>start duplicates, recurse from i+1, then undo.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Equal siblings collapse while deeper copies remain

[2,1,2]
[[],[1],[1,2],[1,2,2],[2],[2,2]]