Count of unique subsets from a set having repeated elements
Last Updated :
27 May, 2022
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 <bits/stdc++.h>
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<Integer, Integer> 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<Integer, Integer> 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
<script>
// JavaScript code for the above approach
// Function to find number of unique subsets
function countNumberofUniqueSubsets(A, N)
{
// Creating a map to store
// frequency of elements
let m = new Map();
// Filling map
for (let i = 0; i < N; i++)
{
// Counting frequency of each elements
if (m.has(A[i])) {
m.set(A[i], m.get(A[i])+ 1)
}
else {
m.set(A[i], 1)
}
}
// Finding product of (frequency+1)
// for each elements
let subsets = 1;
for (let value of m.values())
subsets = subsets * (value + 1);
document.write(subsets);
}
// Driver Code
let arr = [1, 2, 2];
let N = arr.length;
// Function Call
countNumberofUniqueSubsets(arr, N);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Partition an array into two subsets with equal count of unique elements Given an array arr[] consisting of N integers, the task is to partition the array into two subsets such that the count of unique elements in both the subsets is the same and for each element, print 1 if that element belongs to the first subset. Otherwise, print 2. If it is not possible to do such a
13 min read
Count of subsets of integers from 1 to N having no adjacent elements Given an integer N, the task is to count the number of subsets formed from an array of integers from 1 to N which doesn't contain adjacent elements. A subset can't be chosen if it satisfies the non-adjacent element condition, but it is possible to add more elements.Examples: Input: N = 4 Output: 3 E
7 min read
Find all Unique Subsets of a given Set Given an array A[] of positive integers, print all the unique non-empty subsets of the array Note: The set can not contain duplicate elements, so any repeated subset should be considered only once in the output. Examples: Input: A[] = {1, 5, 6}Output: {{1}, {1, 5}, {1, 6}, {5}, {5, 6}, {6}, {1, 5,
15+ min read
Count subarrays having exactly K elements occurring at least twice Given an array arr[] consisting of N integers and a positive integer K, the task is to count the number of subarrays having exactly K elements occurring at least twice. Examples: Input: arr[] = {1, 1, 1, 2, 2}, K = 1Output: 7Explanation: The subarrays having exactly 1 element occurring at least twic
11 min read
Count no. of ordered subsets having a particular XOR value Given an array arr[] of n elements and a number K, find the number of ordered subsets of arr[] having XOR of elements as K This is a modified version of this problem. So it is recommended to try that problem before.Examples: Input: arr[] = {6, 9, 4, 2}, k = 6 Output: 2 The subsets are {4, 2}, {2, 4}
11 min read