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.
Example 1
Input: arr[] = [1]
Output: [] [1]
Explanation: Only the empty set and the single element subset are possible.
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
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.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 2
1 2 12 22 122
1 1 2
1 11 2 12 112