Skip to content

90. Subsets II

Edited by Charles

Medium · Python · Backtrack

Problem

Read the original problem on LeetCode.

The official page contains the problem statement, examples, and constraints.

Explanation, examples & other approaches

Core idea

Recall 40. Combination Sum II and 78. Subsets for the core ideas. The below condition

i > startIndex and nums[i] == nums[i - 1]:

Original study notes follow, including intermediate snippets and earlier attempts.

Note:

  1. Recall 40. Combination Sum II and 78. Subsets for the core ideas.
  2. The below condition

    i > startIndex and nums[i] == nums[i - 1]:
    

    ensures, within the for loop, duplicates are checked, for example, [1, 2, 2] with startIndex = 0, the current possible paths are [1, 2], [1, 2], if we do not consider the deeper recurrence with parsing i + 1 to startIndex.

Study record
Field Value
Attempts 1
Mastery ❚
The Latest Attempt January 24, 2026

Python solution

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        result = []
        nums.sort()
        self.backtrack(nums, 0, [], result)
        return result

    def backtrack(self, nums, startIndex, path, result):
        result.append(path[:])

        for i in range(startIndex, len(nums)):
            if i > startIndex and nums[i] == nums[i - 1]:
                continue

            path.append(nums[i])
            self.backtrack(nums, i + 1, path, result)
            path.pop()

Back to Day 21 Backtrack III · All Algorithm notes