Minimum Subsets with Distinct Elements
Last Updated :
27 Feb, 2025
You are given an array of n-element. You have to make subsets from the array such that no subset contain duplicate elements. Find out minimum number of subset possible.
Examples :
Input : arr[] = {1, 2, 3, 4}
Output :1
Explanation : A single subset can contains all values and all values are distinct.
Input : arr[] = {1, 2, 3, 3}
Output : 2
Explanation : We need to create two subsets {1, 2, 3} and {3} [or {1, 3} and {2, 3}] such that both subsets have distinct elements.
[Naive Solution] - Nested Loops - O(n^2) Time and O(1) Space
Let us take a look at few observations.
- If all elements are distinct, we need to make only one subset.
- If all elements are same, we need make n subsets.
- If an element appears twice, and all other are distinct, we need to make two subsets,
Did you see any pattern?
We basically need to find the most frequent element in the array. The result is equal to the frequency of the most frequent element. Since we have to create a subset such that each element in a subset is unique that means that all the repeating elements should be kept in a different set. Hence the maximum no subsets that we require is the frequency of the maximum time occurring element.
Ex -> { 1 , 2 , 1 , 2 , 3 , 3 , 2 , 2 }
here
Frequency of 1 -> 2
Frequency of 2 -> 4
Frequency of 3 -> 2
Since the frequency of 2 is maximum hence we need to have at least 4 subset to keep all the 2 in different subsets and rest of element can be occupied accordingly.
The naive approach involves using two nested loops: the outer loop picks each element, and the inner loop counts the frequency of the picked element. This method is straightforward but inefficient.
C++
// CPP program to find the most frequent element in an array.
#include <bits/stdc++.h>
using namespace std;
int minSubsets(vector<int> &arr)
{
int n = arr.size(), maxcount = 0;
int res;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}
if (count > maxcount) {
maxcount = count;
res = arr[i];
}
}
return res;
}
// Driver program
int main()
{
vector<int> arr = { 40, 50, 30, 40, 50, 30, 30 };
cout << minSubsets(arr);
return 0;
}
Java
class GfG {
static int minSubsets(int[] arr) {
int n = arr.length, maxCount = 0, res = arr[0];
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) count++;
}
if (count > maxCount) {
maxCount = count;
res = arr[i];
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {40, 50, 30, 40, 50, 30, 30};
System.out.println(minSubsets(arr));
}
}
Python
def min_subsets(arr):
n, max_count, res = len(arr), 0, arr[0]
for i in range(n):
count = sum(1 for j in range(n) if arr[i] == arr[j])
if count > max_count:
max_count = count
res = arr[i]
return res
arr = [40, 50, 30, 40, 50, 30, 30]
print(min_subsets(arr))
JavaScript
function minSubsets(arr) {
let n = arr.length, maxCount = 0, res = arr[0];
for (let i = 0; i < n; i++) {
let count = 0;
for (let j = 0; j < n; j++) {
if (arr[i] === arr[j]) count++;
}
if (count > maxCount) {
maxCount = count;
res = arr[i];
}
}
return res;
}
let arr = [40, 50, 30, 40, 50, 30, 30];
console.log(minSubsets(arr));
[Better Approach] - Using Sorting - O(n Log n) Time and O(1) Space
This method sorts the array first and then finds the maximum frequency by linearly traversing the sorted array. Sorting brings similar elements next to each other, making frequency counting easier.
C++
// CPP program to find the most frequent element
#include <bits/stdc++.h>
using namespace std;
int minSubsets(vector<int>& arr)
{
// Sort the array
sort(arr.begin(), arr.end());
// Find the max frequency using linear traversal
int max_count = 1, res = arr[0], curr_count = 1;
for (int i = 1; i < arr.size(); i++) {
if (arr[i] == arr[i - 1])
curr_count++;
else
curr_count = 1;
if (curr_count > max_count) {
max_count = curr_count;
res = arr[i - 1];
}
}
return res;
}
// Driver program
int main()
{
vector<int> arr = { 40,50,30,40,50,30,30};
cout << minSubsets(arr);
return 0;
}
Java
// Java program to find the most frequent element
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class minSubsets {
public static int minSubsets(int[] arr) {
// Sort the array
Arrays.sort(arr);
// Find the max frequency using linear traversal
int max_count = 1, res = arr[0], curr_count = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1])
curr_count++;
else
curr_count = 1;
if (curr_count > max_count) {
max_count = curr_count;
res = arr[i - 1];
}
}
return res;
}
// Driver program
public static void main(String[] args) {
int[] arr = { 40, 50, 30, 40, 50, 30, 30 };
System.out.println(minSubsets(arr));
}
}
Python
def min_subsets(arr):
# Sort the array
arr.sort()
# Find the max frequency using linear traversal
max_count = 1
res = arr[0]
curr_count = 1
for i in range(1, len(arr)):
if arr[i] == arr[i - 1]:
curr_count += 1
else:
curr_count = 1
if curr_count > max_count:
max_count = curr_count
res = arr[i - 1]
return res
# Driver program
arr = [40, 50, 30, 40, 50, 30, 30]
print(min_subsets(arr))
C#
// C# program to find the most frequent element
using System;
using System.Linq;
public class GfG {
public static int minSubsets(int[] arr) {
// Sort the array
Array.Sort(arr);
// Find the max frequency using linear traversal
int max_count = 1, res = arr[0], curr_count = 1;
for (int i = 1; i < arr.Length; i++) {
if (arr[i] == arr[i - 1])
curr_count++;
else
curr_count = 1;
if (curr_count > max_count) {
max_count = curr_count;
res = arr[i - 1];
}
}
return res;
}
// Driver program
public static void Main() {
int[] arr = { 40, 50, 30, 40, 50, 30, 30 };
Console.WriteLine(minSubsets(arr));
}
}
JavaScript
// JavaScript program to find the most frequent element
function minSubsets(arr) {
// Sort the array
arr.sort((a, b) => a - b);
// Find the max frequency using linear traversal
let max_count = 1, res = arr[0], curr_count = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] === arr[i - 1])
curr_count++;
else
curr_count = 1;
if (curr_count > max_count) {
max_count = curr_count;
res = arr[i - 1];
}
}
return res;
}
// Driver program
const arr = [40, 50, 30, 40, 50, 30, 30];
console.log(minSubsets(arr));
[Expected Approach] - Using Hashing - O(n) Time and O(n) Space
Using a hash table, this approach stores each element's frequency and then finds the element with the maximum frequency.
C++
// CPP program to find the most frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;
int minSubsets(int arr[], int n)
{
// Insert all elements in hash.
unordered_map<int, int> freq;
for (int i = 0; i < n; i++)
freq[arr[i]]++;
// find the max frequency
int max_count = 0, res = -1;
for (auto i : freq) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
return res;
}
int main()
{
int arr[] = {40,50,30,40,50,30,30 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minSubsets(arr, n);
return 0;
}
Java
// Java program to find the most frequent element
// in an array
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class GFG {
static int minSubsets(int arr[], int n)
{
// Insert all elements in hash
Map<Integer, Integer> hp
= new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
int key = arr[i];
if (hp.containsKey(key)) {
int freq = hp.get(key);
freq++;
hp.put(key, freq);
}
else {
hp.put(key, 1);
}
}
// find max frequency.
int max_count = 0, res = -1;
for (Entry<Integer, Integer> val : hp.entrySet()) {
if (max_count < val.getValue()) {
res = val.getKey();
max_count = val.getValue();
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 40, 50, 30, 40, 50, 30, 30 };
int n = arr.length;
System.out.println(minSubsets(arr, n));
}
}
Python
# Python3 program to find the most
# frequent element in an array.
import math as mt
def minSubsets(arr, n):
# Insert all elements in Hash.
Hash = dict()
for i in range(n):
if arr[i] in Hash.keys():
Hash[arr[i]] += 1
else:
Hash[arr[i]] = 1
# find the max frequency
max_count = 0
res = -1
for i in Hash:
if (max_count < Hash[i]):
res = i
max_count = Hash[i]
return res
arr = [ 40,50,30,40,50,30,30]
n = len(arr)
print(minSubsets(arr, n))
C#
// C# program to find the most
// frequent element in an array
using System;
using System.Collections.Generic;
class GFG
{
static int minSubsets(int []arr,
int n)
{
// Insert all elements in hash
Dictionary<int, int> hp =
new Dictionary<int, int>();
for (int i = 0; i < n; i++)
{
int key = arr[i];
if(hp.ContainsKey(key))
{
int freq = hp[key];
freq++;
hp[key] = freq;
}
else
hp.Add(key, 1);
}
// find max frequency.
int min_count = 0, res = -1;
foreach (KeyValuePair<int,
int> pair in hp)
{
if (min_count < pair.Value)
{
res = pair.Key;
min_count = pair.Value;
}
}
return res;
}
static void Main ()
{
int []arr = new int[]{40,50,30,40,50,30,30};
int n = arr.Length;
Console.Write(minSubsets(arr, n));
}
}
JavaScript
// Javascript program to find
// the most frequent element
// in an array.
function minSubsets(arr, n)
{
// Insert all elements in hash.
var hash = new Map();
for (var i = 0; i < n; i++) {
if (hash.has(arr[i]))
hash.set(arr[i], hash.get(arr[i]) + 1)
else hash.set(arr[i], 1)
}
// find the max frequency
var max_count = 0, res = -1;
hash.forEach((value, key) => {
if (max_count < value) {
res = key;
max_count = value;
}
});
return res;
}
var arr = [ 40, 50, 30, 40, 50, 30, 30 ];
var n = arr.length;
console.log(minSubsets(arr, n));
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem