Back to Practice Dashboard
Top 150 InterviewEasy

Subsets II

Learn how to solve the 'Subsets II' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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.

Implement a function subsetsWithDup(nums: list) -> list.

Constraints
  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

Examples

Example 1
Input
[1,2,2]
Output
[[],[1],[1,2],[1,2,2],[2],[2,2]]
Explanation

Unlike the basic subsets problem, [1,2,2] has duplicate 2's. We skip duplicate subsets like having two copies of [2].

Example 2
Input
[0]
Output
[[],[0]]
Explanation

Same as basic subsets for a single element.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor