Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Subsets with Duplicates

Medium Acceptance 74.00% Points 30.00

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.

Hints
Hint 1
N/A
Constraints
  • 1 = n = 20
  • -10? = arr[i] = 10?
Companies
Samsung Nvidia
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions