Subsets II

Difficulty: Medium

Given a list of numbers that may contain repeats, return every possible subset (including the empty one and the full list). The answer must not contain two identical subsets (same values, same counts).

Examples

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

Without de-duplication you'd also get a second [1,2] and a second [2] (from picking either of the two 2s) - those repeats are left out.

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

The three 4s are indistinguishable, so a subset is defined only by how many 4s it contains (0-3), not which ones.

Constraints

  • 1 <= nums.length <= 10

  • -10 <= nums[i] <= 10

Approach

This is the plain subsets search, except duplicate values can reach the same subset by two paths - choosing "the first 2" or "the second 2" gives an identical subset.

The brute-force fix runs the normal search and filters duplicates afterward, for example by turning each subset into a sorted string key and keeping only the first subset per key.

A more direct fix never generates duplicates. Sort the input so equal values are neighbors. When choosing what to include next at a step, skip a value equal to the one you just decided not to include at that step. This one rule stops the search from re-exploring a branch it has already covered.

Solutions

Brute Force — Generate Then De-duplicate

Run the plain include/exclude backtracking, ignoring duplicates. This produces repeats of subsets reached through different equal values. Afterward, key each subset on its sorted, comma-joined values and keep one copy per key.

function subsetsWithDup(nums) {
  const all = [];
  const current = [];

  function backtrack(index) {
    if (index === nums.length) {
      all.push([...current]);
      return;
    }
    current.push(nums[index]);
    backtrack(index + 1);
    current.pop();
    backtrack(index + 1);
  }

  backtrack(0);

  const seen = new Set();
  const result = [];
  for (const subset of all) {
    const key = [...subset].sort((a, b) => a - b).join(",");
    if (!seen.has(key)) {
      seen.add(key);
      result.push(subset);
    }
  }

  return result;
}

Time: O(2^n * n) to generate every subset, plus O(2^n * n) to key and de-duplicate them · Space: O(2^n * n) for the raw subsets and the de-duplication set

Optimal — Sort and Skip Duplicate Branches

Sort the input, then build subsets by choosing which index to include next. Skip a value equal to the previous element at the same step - its branch is already covered by the first occurrence.

function subsetsWithDup(nums) {
  const sorted = [...nums].sort((a, b) => a - b);
  const result = [];
  const current = [];

  function backtrack(start) {
    result.push([...current]);

    for (let i = start; i < sorted.length; i++) {
      if (i > start && sorted[i] === sorted[i - 1]) continue; // skip duplicate at this level

      current.push(sorted[i]);
      backtrack(i + 1);
      current.pop();
    }
  }

  backtrack(0);
  return result;
}

Time: O(2^n * n) · Space: O(n) auxiliary (recursion + current array), plus O(2^n * n) output