Subsets With Duplicates

Implement subsetsWithDup

You're given an array nums that may repeat values. Produce every distinct subset it has — no two entries in the result may contain the same multiset of values, even though the same value might sit at more than one position in the input. Treating every position as independent (as if all values were unique) overcounts: two different positions holding the same value can build the exact same subset. The fix is to only ever pick "the next occurrence of a repeated value" going deeper into a choice already made, never as a fresh alternative at the same decision point.

Example 1:

Input: nums = [1,2,2]

Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]

Example 2:

Input: nums = [0]

Output: [[],[0]]

Example 3:

Input: nums = [1,1,1]

Output: [[],[1],[1,1],[1,1,1]]

+ 8 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 10
  • -10 ≤ nums[i] ≤ 10
  • nums may contain repeated values

nums =

[1, 2, 2]