Skip to contentKEEP THE CRAFTTHE SANCTUARY

Unique Subsets With Duplicate Inputs

Return every subset exactly once when the input contains repeated values.

The problem

Implement Solution.subsetsWithDup(nums). nums may contain equal values. Return every distinct subset of nums, including the empty subset, with no subset listed twice. For example, [1, 2, 2] has the subsets [], [1], [2], [1, 2], [2, 2] and [1, 2, 2]; [1, 2] counts once even though there are two 2s. The subsets can be in any order, and so can the values inside each subset. If nums is empty, return [[]]. Constraints: 0 to 10 integers, each from -10 to 10. You may sort the input.

Starter code

class Solution:
    def subsetsWithDup(self, nums):
        pass

Public examples

  1. nums = [1, 2, 2]gives[[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
  2. nums = [0, 0]gives[[], [0], [0, 0]]
  3. nums = [2, 1, 2]gives[[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.