Unique Subsets | Practice | GeeksforGeeks

Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets.
Note: Each subset should be sorted.
Example 1:
Input: N = 3, arr[] = {2,1,2}
Output:(),(1),(1 2),(1 2 2),(2),(2 2)
Explanation: 
All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2)
After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) 
Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2)

Example 2:
Input: N = 4, arr[] = {1,2,3,3}
Output: (),(1),(1 2),(1 2 3)
(1 2 3 3),(1 3),(1 3 3),(2),(2 3)
(2 3 3),(3),(3 3)
Your Task:
Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. 
Expected Time Complexity: O(2N).
Expected Auxiliary Space: O(2N * X), X = Length of each subset.
Constraints:
1 ≤ N ≤ 12
1 ≤ arr[i] ≤ 9
+1
atulsharma28231 hour ago
public:   //Function to find all possible unique subsets.   set<vector<int>>st;     void generate(vector<int>arr,vector<int>&v)   {       if(arr.size()==0)       {           sort(v.begin(),v.end());           st.insert(v);           return;       }       else       {           vector<int>v1,v2;           v1=v;           v2=v;           v2.push_back(arr[0]);           arr.erase(arr.begin()+0);           generate(arr,v1);           generate(arr,v2);       }   }   vector<vector<int> > AllSubsets(vector<int> arr, int n)   {       // code here        vector<int>v;       generate(arr,v);       vector<vector<int>>vac;       for(auto &v:st)       {           vac.push_back(v);       }       return vac;   }
0
rockzthakur1998091 day ago
 vector<vector<int> > AllSubsets(vector<int> arr, int n)
   {
       vector<vector<int>>v;
       int t=pow(2,n);
       set<vector<int>>s;
       for(int i=0;i<t;i++)
       {
           vector<int>a;
           for(int j=0;j<n;j++)
           {
               if(i&(1<<j))
               {
                   a.push_back(arr[j]);
               }
           }
           sort(a.begin(),a.end());
           s.insert(a);
       }
       
       for(auto it:s)
       {
           v.push_back(it);
       }
       return v;
   }
+1
aloksinghbais022 weeks ago
C++ solution having time complexity as O(2^N) and space complexity as O(2^N) is as follows :- 
 
Execution Time :- 0.77 / 1.32 sec
 
set<vector<int>> s;      void helper(vector<int> &arr,int ind,vector<int> output){       if(ind == arr.size()){           s.insert(output);           return;       }              int ele = arr[ind];       helper(arr,ind+1,output);       output.push_back(ele);       helper(arr,ind+1,output);   }      vector<vector<int> > AllSubsets(vector<int> arr, int n){       sort(arr.begin(),arr.end());       vector<int> output;       helper(arr,0,output);              vector<vector<int>> ans;       set<vector<int>>::iterator iter;       for(iter = s.begin(); iter != s.end(); iter++){           ans.push_back(*iter);       }              return (ans);   }
0
abytespaceneeded3 weeks ago
class Solution
{
    public:
    //Function to find all possible unique subsets.
    vector<vector<int> > AllSubsets(vector<int> arr, int n)
    {
        // code here 
        vector<vector<int>> ans;
        
        for(int i=0;i<(1<<n);i++)
        {
            vector<int> temp;
            for(int j=0;j<n;j++)
            {
                if((i & (1<<j)))
                    temp.push_back(arr[j]);
            }
            
            ans.push_back(temp);
            temp.clear();
        }
        
        for(int i=0;i<ans.size();i++)
            sort(ans[i].begin(),ans[i].end());
            
        set<vector<int>> s;
        for(int i=0;i<ans.size();i++)
        {
            s.insert(ans[i]);
        }
        
        ans.clear();
        
        for(auto it: s)
        {
            ans.push_back(it);
        }
        
        return ans;
    }
};

0
abytespaceneeded
This comment was deleted.
0
kuldeepy104591 month ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
// } Driver Code Endsclass Solution{   public:   //Function to find all possible unique subsets.    vector<vector<int>> ans;    set<vector<int> > s;    vector<int> vec;            void helper(vector<int>arr,int i,int n){        if(i==n){            s.insert(vec);            return;        }        vec.push_back(arr[i]);        helper(arr,i+1,n);        vec.pop_back();        helper(arr,i+1,n);    }       vector<vector<int> > AllSubsets(vector<int> arr, int n)   {       // code here        sort(arr.begin(),arr.end());       helper(arr,0,n);       for(auto x:s){           ans.push_back(x);       }       return ans;   }};
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--){       int n;       cin>>n;       vector<int> A;       int x;       for(int i=0;i<n;i++){           cin>>x;           A.push_back(x);       }       Solution obj;       vector<vector<int> > result = obj.AllSubsets(A,n);       // printing the output       for(int i=0;i<result.size();i++){           cout<<'(';           for(int j=0;j<result[i].size();j++){               cout<<result[i][j];               if(j<result[i].size()-1)                   cout<<" ";           }           cout<<")";       }       cout<<"\n";   }}  
 // } Driver Code Ends
0
vinamrajha1 month ago
vector<vector<int>>res;
   void helper(vector<int>&arr, vector<int>&temp, int i){
       res.push_back(temp);
       for(int j =i; j<arr.size(); ++j){
           /*as we're prompt to only consider unique elements, we skip the duplicates. Below line of code does that part*/
           if(j!=i && arr[j]==arr[j-1])continue; 
           temp.push_back(arr[j]);
           helper(arr, temp, j+1);
           temp.pop_back();
       }
   }
   vector<vector<int>>AllSubsets(vector<int> arr, int n)
   {
       // code here
       sort(arr.begin(), arr.end());
       vector<int>temp;
       helper(arr, temp, 0);
       return res;
   }
+1
prakashsuraj4141
This comment was deleted.
+2
rajatsingh16032 months ago
void recursive(vector<int> arr,vector<vector<int>> &ans,vector<int> &ds,int indx){       ans.push_back(ds);       for(int i = indx; i<arr.size(); i++){           if(i != indx && arr[i] == arr[i-1])continue;           ds.push_back(arr[i]);           recursive(arr,ans,ds,i+1);           ds.pop_back();       }   }      vector<vector<int> > AllSubsets(vector<int> arr, int n)   {       // code here        vector<vector<int>> ans;       vector<int> ds;       sort(arr.begin(),arr.end());       recursive(arr,ans,ds,0);       return ans;   }
0
lindan1232 months ago
 public:
    //Function to find all possible unique subsets.
    vector<vector<int>> ans;
    set<vector<int> > s;
    vector<int> vec;
    void solve(vector<int> arr,int i,int n)
    {
        if(i==n)
        {
            s.insert(vec);
            return ;
        }
        
        vec.push_back(arr[i]);
        solve(arr,i+1,n);
        vec.pop_back();
        solve(arr,i+1,n);
    }
    vector<vector<int> > AllSubsets(vector<int> arr, int n)
    {
        
       
        sort(arr.begin(),arr.end()); 
      
        solve(arr,0,n);
        for(auto x :s)
        {
         ans.push_back(x);
        }
        return ans;
    }
Time Taken : 0.7sec
Cpp
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.