Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
Example 2:
Input: nums = [0] Output: [[],[0]]
Constraints:
1 <= nums.length <= 10-10 <= nums[i] <= 10Problem Overview: Given an integer array that may contain duplicates, generate all possible subsets (the power set) while ensuring that the result does not contain duplicate subsets. The challenge is not generating subsets—it’s avoiding repeated combinations when the input contains duplicate values.
Approach 1: Backtracking with Sorting (Time: O(2^n), Space: O(n))
This approach uses backtracking to explore every subset while carefully skipping duplicates. Start by sorting the array so duplicate values appear next to each other. During the recursive DFS, iterate through elements starting from the current index and append each element to the current subset. If the current element is the same as the previous element and appears at the same recursion depth, skip it to prevent generating the same subset again. Each recursive call either includes an element or moves forward to explore new combinations.
The key insight is that sorting groups duplicates together, which makes it possible to skip redundant branches in the recursion tree. Without this check, duplicate subsets would appear multiple times. The recursion stack depth is at most n, and every valid subset is produced exactly once. Since the total number of subsets in the worst case is 2^n, the time complexity is O(2^n) and the auxiliary recursion space is O(n). This method is commonly used in subset and combination problems involving duplicates.
Approach 2: Iterative Subset Construction (Time: O(2^n), Space: O(2^n))
This approach builds subsets iteratively using an expanding result list. Start with a single empty subset. After sorting the array, iterate through each element and extend the existing subsets by adding the current number. The tricky part is handling duplicates. When the current element is the same as the previous one, only extend the subsets that were added during the previous step rather than all subsets. This prevents duplicate combinations from forming.
The algorithm maintains a boundary that marks where new subsets began in the previous iteration. When encountering a duplicate element, iteration starts from that boundary instead of from the beginning of the result list. Each step effectively doubles or partially expands the subset list. Because every subset is constructed exactly once, the time complexity remains O(2^n) with space O(2^n) for storing the result. This method works well when you prefer iterative logic over recursion and is still rooted in operations on an array.
Recommended for interviews: The backtracking approach with sorting is the most commonly expected solution. Interviewers want to see that you recognize duplicates as a pruning condition in the recursion tree. Writing the brute recursive structure first demonstrates understanding of subset generation, while adding the duplicate-skip condition shows mastery of backtracking patterns. The iterative method is a strong alternative when discussing different ways to construct the power set.
In this approach, the problem of subsets with duplicates is solved by first sorting the array. This assists in easily identifying and omitting duplicates. The backtracking method is utilized to construct all possible subsets, ensuring no duplicate subsets are generated by skipping duplicate numbers during recursive backtracking.
The solution uses a sorting-based backtracking method. The array is sorted to handle duplicates easily. A recursive backtracking function is used to generate subsets. Before adding a number to the current subset, we check whether it is a duplicate of the previously considered number in the same recursion level. This avoids generating duplicate subsets.
C++
Java
Python
C#
JavaScript
The time complexity of this solution is O(2^n * n) due to the number of subsets (2^n) and time taken to convert each subset to the result. The space complexity is O(n), where n is the length of the array, due to the space required for the subset building.
This approach iteratively constructs the power set by extending previously generated sets with each new number. During the process, duplicates are skipped by comparing the current number with the previous one. This ensures subsets generated in each iteration are unique.
The C solution utilizes iterative sets through a sorted array. It dynamically expands the subsets using the current number while bypassing duplicates by monitoring the indices of additions, skipping similar additions through index boundaries.
C++
Java
Python
C#
JavaScript
The time complexity is O(2^n * n); space is O(2^n * n) as it iteratively builds the power set using subsets directly.
| Approach | Complexity |
|---|---|
| Approach 1: Backtracking with Sorting | The time complexity of this solution is O(2^n * n) due to the number of subsets (2^n) and time taken to convert each subset to the result. The space complexity is O(n), where n is the length of the array, due to the space required for the subset building. |
| Approach 2: Iterative Subset Construction | The time complexity is O(2^n * n); space is O(2^n * n) as it iteratively builds the power set using subsets directly. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Sorting | O(2^n) | O(n) | Best interview approach. Clean recursion with duplicate skipping. |
| Iterative Subset Construction | O(2^n) | O(2^n) | Useful when avoiding recursion or when building subsets step-by-step. |
L11. Subset Sum II | Leetcode | Recursion • take U forward • 380,741 views views
Watch 9 more video solutions →Practice Subsets II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor