Python - Maximum frequency in Tuple
Last Updated :
16 May, 2023
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
# 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
# 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:
- 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):
# 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)))
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
# 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))
OutputThe 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
# 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.
Similar Reads
Python | Values Frequency till Maximum K
One of the problems that is basically a subproblem for many complex problems, finding numbers smaller than certain numbers in a list in Python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method 1 : Naive method The most common way thi
6 min read
Python | Maximum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Elements frequency in Tuple Matrix
Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved. Inp
5 min read
Python | Index Maximum among Tuples
Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Letâs discuss certain ways in which this task can be performed. Metho
6 min read
Python - Elements frequency in Tuple
Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Maximum Frequency Character in String - Python
The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string "hello world", the character 'l' appears the most frequently (3 times).Using collection.CounterCounter class from the collections modul
3 min read
Python - Concatenate Maximum Tuples
Given a tuple list with string and its magnitude, the task is to write a python program to join all the strings with maximum magnitudes. Examples: Input : test_list = [("Gfg is best", 8), ("gfg is good", 7), ("for", 2), ("for all geeks", 8)]Output : "Gfg is best for all geeks" Explanation : 8 is max
8 min read
Python - Values Frequency Index List
Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of
4 min read
Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Python - Elements Frequency in Mixed Nested Tuple
Sometimes, while working with Python data, we can have a problem in which we have data in the form of nested and non-nested forms inside a single tuple, and we wish to count the element frequency in them. This kind of problem can come in domains such as web development and Data Science. Let's discus
8 min read