Python – Maximum frequency in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let’s discuss certain ways in which this task can be performed.
Input : test_tuple = (6, 7, 10, 11, 10)
Output : 10
Input : test_tuple = (5, 5, 5)
Output : 5
Method #1: Using count() + loop
The combination of the above functions can be used to solve this problem. This is a brute-force approach to solve this problem. In this, we use count() to perform the counting of elements.
Python3
test_tuple = ( 6 , 7 , 8 , 6 , 7 , 10 )
print ( "The original tuple : " + str (test_tuple))
cnt = 0
res = test_tuple[ 0 ]
for ele in test_tuple:
curr_freq = test_tuple.count(ele)
if (curr_freq > cnt):
cnt = curr_freq
res = ele
print ( "Maximum element frequency tuple : " + str (res))
|
Output :
The original tuple : (6, 7, 8, 6, 7, 10)
Maximum element frequency tuple : 6
Method #2: Using max() + Counter() + lambda
The combination of the above functions can be used to solve this problem. In this, we use Counter() to find the frequency of all elements and max() is used to find the maximum of it.
Python3
from collections import Counter
test_tuple = ( 6 , 7 , 8 , 6 , 7 , 10 )
print ( "The original tuple : " + str (test_tuple))
res = max (Counter(test_tuple).items(), key = lambda ele: ele[ 1 ])
print ( "Maximum element frequency tuple : " + str (res[ 0 ]))
|
Output :
The original tuple : (6, 7, 8, 6, 7, 10)
Maximum element frequency tuple : 6
Method #3: Using the statistics mode() function
This approach defines a function max_frequency_in_tuple() which uses statistics.mode() function to find the element with the highest frequency in a given tuple. It then returns the element with the highest frequency. The print() statements at the bottom of the code test the function with two sample inputs and print the outputs.
Step-by-step approach:
- Use the statistics mode() function to find the element with the highest frequency in the tuple.
- Return the element with the highest frequency.
Python3
import statistics
def max_frequency_in_tuple(test_tuple):
max_freq_element = statistics.mode(test_tuple)
return max_freq_element
print (max_frequency_in_tuple(( 6 , 7 , 10 , 11 , 10 )))
print (max_frequency_in_tuple(( 5 , 5 , 5 )))
|
Time complexity: O(nlogn), where n is the length of the array
Auxiliary Space: O(1)
Method 4: Using a dictionary
Step-by-step approach:
- Initialize an empty dictionary freq_dict to store the frequency of each element.
- Iterate through the elements of the tuple using a for loop.
- For each element, use the get() method of the dictionary to retrieve its current frequency. If the element is not yet in the dictionary, the get() method returns a default value of 0. Increment the retrieved frequency by 1 and store the new frequency in the dictionary.
- Use the max() function to find the element with the maximum frequency. The key argument of max() specifies a function to extract a comparison key from each dictionary key. In this case, we use freq_dict.get as the key function, which returns the frequency of each element in the dictionary.
- Print the result.
Python3
test_tuple = ( 6 , 7 , 8 , 6 , 7 , 10 )
print ( "The original tuple : " + str (test_tuple))
freq_dict = {}
for ele in test_tuple:
freq_dict[ele] = freq_dict.get(ele, 0 ) + 1
res = max (freq_dict, key = freq_dict.get)
print ( "Maximum element frequency tuple : " + str (res))
|
Output
The original tuple : (6, 7, 8, 6, 7, 10)
Maximum element frequency tuple : 6
Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(n), where n is the length of the tuple.
Method 5: Using numpy
- Import the numpy module
- Convert the tuple to a numpy array using the np.array() function
- Use the np.unique() function to get the unique elements and their frequency counts in the array
- Find the index of the maximum frequency count using the np.argmax() function
- Get the corresponding unique element using the np.unique() function with the return_counts argument set to True
- Print the element with the maximum frequency count
Python3
import numpy as np
test_tuple = ( 6 , 7 , 8 , 6 , 7 , 10 )
print ( "The original tuple : " + str (test_tuple))
unique_elements, element_counts = np.unique(np.array(test_tuple), return_counts = True )
max_freq_idx = np.argmax(element_counts)
res = unique_elements[max_freq_idx]
print ( "Maximum element frequency tuple : " + str (res))
|
Output:
The original tuple : (6, 7, 8, 6, 7, 10)
Maximum element frequency tuple : 6
Time complexity: O(n*log(n)), where n is the length of the input tuple.
Auxiliary space: O(n), where n is the length of the input tuple.
Last Updated :
16 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...