Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Subsets with Duplicates

Medium Acceptance 74% Points 30

You are given an integer array that may contain duplicate elements. Your task is to generate all unique subsets of the array.

The solution set must not contain duplicate subsets, even if the input has repeated values.

Examples
Example 1

Example 1

Input: arr[] = [1]

Output: [] [1]

Explanation: Only the empty set and the single element subset are possible.

Example 2

Example 2

Input: arr[] = [1 2 2]

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

Explanation: Duplicate elements exist, but only unique subset combinations are included.

Example 3

Example 3

Input: arr[] = [1 2 2 3]

Output: [] [1] [2] [3] [1 2] [1 3] [2 2] [2 3] [1 2 2] [1 2 3] [2 2 3] [1 2 2 3]

Explanation: Subsets are formed while avoiding repeated combinations caused by duplicates.

Constraints
  • 1 = n = 20
  • -10? = arr[i] = 10?
Topics
Recursion
Companies
Samsung Nvidia
Added 19 Jul 2026 74% acceptance
Ln 1, Col 1Sign in to save your code
1 2 2
 
1 2 12 22 122