InferenceAI

Enter at least two characters to search.

Start free

All Subsets With Duplicates

mediumRecursion & Backtracking25 min25 XP

Return every distinct subset of nums. Sort each subset ascending and return the subsets in lexicographic order, so the answer is unique.

Requirements

  • Avoid generating duplicates in the first place: sort first and skip equal values at the same recursion depth.

Examples

EXAMPLE WALKTHROUGHone duplicate

A guided view of the published example, not a trace of your code.

INPUT[[1,2,2]]
TASKall_subsets_with_duplicates()
EXPECTED OUTPUT[[],[1],[1,2],[1,2,2],[2],[2,2]]

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [1,2,2]

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

Explanation: one duplicate

Input: nums = [0]

Output: [[],[0]]

Explanation: single

Input: nums = []

Output: [[]]

Explanation: empty

Hint 1

Sort nums. At each depth, loop over the remaining choices and skip a value equal to the previous choice at that depth.

Topics

DSARecursion & BacktrackingPython