Subsets II

mediumBacktrackingsort + skip duplicates while backtrackingSolve on LeetCode#90

Part of Practice: Duplicates & Grids

Given an integer array that may contain duplicate values, return every possible subset (the power set) — but the result must not contain duplicate subsets. Subsets and their elements may appear in any order.

For example, with input [1, 2, 2], the subset [1, 2] can be formed two ways (using either 2), yet it must appear only once in the output. The array holds at most 10 elements.

Examples

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

Six unique subsets. Plain power-set enumeration would produce [1, 2] and [2] twice each — once per copy of 2 — so duplicates must be skipped.

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

No duplicates in the input, so this behaves exactly like the basic Subsets problem.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.