GeeksforGeeks App
Open App
Browser
Continue

# Count of unique subsets from a set having repeated elements

Given an array arr[] of size N. The task is to count the number of unique subsets.

Examples:

Input: arr[] = {1, 2, 2}
Output: 6
Explanation: Total possible subsets of this set  = 2³= 8.
Following are the all 8 subsets formed from arr[].
{}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2}.
These are all possible subsets out of which {2} and {1, 2} are repeated.
Therefore there are only 6 unique subsets of given set.

Input: arr[] = {1, 3, 3, 4, 4, 4}
Output: 24

Naive Approach: In the basic approach, create all the subsets of the set and keep storing them in a std::set that stores only unique elements. But this isn’t a time-efficient approach.

Time Complexity: O(2N)
Auxiliary Space: O(N * 2N-1)

Efficient Approach: It can be observed that it is not required to find all the subsets. The only concern is to find the count of unique subsets. On the basis of how many times each element is contributing in unique subsets, it can be done by mathematical manipulations and finally ending up with a formula.

Follow the observation below to arrive at the formula.

For each unique value vali say the frequency of that element is freq[vali].
So each unique value has (freq[vali] + 1) to be present in a unique subset
because it can be present 0 times, 1 time, 2 times . . . freq[vali] times in a subset.
Now this is true for all such unique elements.
Therefore, The number of unique subsets of a set  = Product of (frequency+1) of each element.

Below is the implementation of the above approach.

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `// Function to find number of unique subsets``void` `countNumberofUniqueSubsets(``int` `A[],``                                ``int` `N)``{``    ``// Creating a map to store``    ``// frequency of elements``    ``map<``int``, ``int``> m;` `    ``// Filling map``    ``for` `(``int` `i = 0; i < N; i++) {``        ``// Counting frequency of each elements``        ``m[A[i]]++;``    ``}` `    ``// Finding product of (frequency+1)``    ``// for each elements``    ``int` `subsets = 1;` `    ``for` `(``auto``& value : m)``        ``subsets *= (value.second + 1);` `    ``cout << subsets;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 2 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``countNumberofUniqueSubsets(arr, N);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG {` `  ``// Function to find number of unique subsets``  ``static` `void` `countNumberofUniqueSubsets(``int` `A[],``                                         ``int` `N)``  ``{``    ` `    ``// Creating a map to store``    ``// frequency of elements``    ``HashMap m = ``new` `HashMap<>();` `    ``// Filling map``    ``for` `(``int` `i = ``0``; i < N; i++) {``      ``// Counting frequency of each elements``      ``if` `(m.containsKey(A[i]))``      ``{``        ``m.put(A[i], m.get(A[i]) + ``1``);``      ``}``      ``else``      ``{``        ``m.put(A[i], ``1``);``      ``}``    ``}` `    ``// Finding product of (frequency+1)``    ``// for each elements``    ``int` `subsets = ``1``;` `    ``for` `(Map.Entry value : m.entrySet())``      ``subsets *= (value.getValue() + ``1``);` `    ``System.out.print(subsets);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args) {``    ``int` `arr[] = { ``1``, ``2``, ``2` `};``    ``int` `N = arr.length;` `    ``// Function Call``    ``countNumberofUniqueSubsets(arr, N);``  ``}``}` `// This code is contributed by hrithikgarg03188.`

## Python

 `# Python code for the above approach` `# Function to find number of unique subsets``def` `countNumberofUniqueSubsets(A, N):` `    ``# Creating a map to store``    ``# frequency of elements``    ``m ``=` `dict``()` `    ``# Filling map``    ``for` `i ``in` `range``(N):` `        ``# Counting frequency of each elements``        ``if` `(A[i] ``in` `m):``            ``m[A[i]] ``+``=` `1``        ``else``:``            ``m[A[i]] ``=` `1` `    ``# Finding product of (frequency+1)``    ``# for each elements``    ``subsets ``=` `1` `    ``for` `value ``in` `m.values():``        ``subsets ``=` `subsets ``*` `(value ``+` `1``)` `    ``print``(subsets)` `# Driver Code``arr ``=` `[``1``, ``2``, ``2``]``N ``=` `len``(arr)` `# Function Call``countNumberofUniqueSubsets(arr, N)` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG {` `  ``// Function to find number of unique subsets``  ``static` `void` `countNumberofUniqueSubsets(``int` `[]A, ``int` `N)``  ``{` `    ``// Creating a map to store``    ``// frequency of elements``    ``Dictionary<``int``, ``int``> m =``      ``new` `Dictionary<``int``, ``int``>();` `    ``// Filling map``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `      ``// Counting frequency of each elements``      ``if` `(m.ContainsKey(A[i]))``      ``{``        ``m[A[i]] = m[A[i]] + 1;``      ``}``      ``else``      ``{``        ``m.Add(A[i], 1);``      ``}``    ``}` `    ``// Finding product of (frequency+1)``    ``// for each elements``    ``int` `subsets = 1;` `    ``foreach``(KeyValuePair<``int``, ``int``> value ``in` `m)``    ``{``      ``subsets *= (value.Value + 1);``    ``}` `    ``Console.Write(subsets);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main () {``    ``int` `[]arr = { 1, 2, 2 };``    ``int` `N = arr.Length;` `    ``// Function Call``    ``countNumberofUniqueSubsets(arr, N);``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`6`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up