Python | Minimum number of subsets with distinct elements using Counter
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.
We have existing solution for this problem please refer Minimum number of subsets with distinct elements link. We will solve this problem quickly in python using Counter(iterable) method. Approach is very simple, calculate frequency of each element in array and print value of maximum frequency because we want each subset to be different and we have to put any repeated element in different subset, so to get minimum number of subset we should have at least maximum frequency number of subsets.
# Python program to find Minimum number of # subsets with distinct elements using Counter # function to find Minimum number of subsets # with distinct elements from collections import Counter def minSubsets(input): # calculate frequency of each element freqDict = Counter(input) # get list of all frequency values # print maximum from it print (max(freqDict.values())) # Driver program if __name__ == "__main__": input = [1, 2, 3, 3] minSubsets(input) |
Output:
2
Recommended Posts:
- Python | Counter Objects | elements()
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Count distinct elements in an array in Python
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Python Counter | Majority Element
- Anagram checking in Python using collections.Counter()
- Python dictionary, set and counter to check if frequencies can become same
- Dictionary and counter in Python to find winner of election
- Python Counter| Find all duplicate characters in string
- Python Counter| Find duplicate rows in a binary matrix
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python Counter to find the size of largest subset of anagram words
- Python program to get all subsets of given size of a set
- Python | Replace list elements with its ordinal number
- Print first n distinct permutations of string using itertools in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



