Open In App

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 code to demonstrate working of
# Maximum frequency in Tuple
# Using loop + count()
 
# Initializing tuple
test_tuple = (6, 7, 8, 6, 7, 10)
 
# Printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Maximum frequency in Tuple
# Using loop + count()
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
 
# Printing result
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 code to demonstrate working of
# Maximum frequency in Tuple
# Using max() + Counter() + lambda
from collections import Counter
 
# Initializing tuple
test_tuple = (6, 7, 8, 6, 7, 10)
 
# Printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Maximum frequency in Tuple
# Using max() + Counter() + lambda
res = max(Counter(test_tuple).items(), key=lambda ele: ele[1])
 
# Printing result
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:




import statistics
 
 
def max_frequency_in_tuple(test_tuple):
    # Use the statistics mode() function to find the element
    # with the highest frequency in the tuple
    max_freq_element = statistics.mode(test_tuple)
 
    # Return the element with the highest frequency
    return max_freq_element
 
 
# Testing the function with given inputs
print(max_frequency_in_tuple((6, 7, 10, 11, 10)))
print(max_frequency_in_tuple((5, 5, 5)))

Output
10
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:




# Python3 code to demonstrate working of
# Maximum frequency in Tuple
# Using dictionary
 
# Initializing tuple
test_tuple = (6, 7, 8, 6, 7, 10)
 
# Printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Maximum frequency in Tuple
# Using dictionary
freq_dict = {}
for ele in test_tuple:
    freq_dict[ele] = freq_dict.get(ele, 0) + 1
 
res = max(freq_dict, key=freq_dict.get)
 
# Printing result
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




# Python code to demonstrate working of
# Maximum frequency in Tuple
# Using numpy
 
# Import numpy module
import numpy as np
 
# Initializing tuple
test_tuple = (6, 7, 8, 6, 7, 10)
 
# Printing original tuple
print("The original tuple : " + str(test_tuple))
 
# Maximum frequency in Tuple
# Using numpy
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]
 
# Printing result
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. 


Article Tags :