Open In App

Python | Maximum frequency character in String

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article gives us the methods to find the frequency of maximum occurring character in a python string. This is quite important utility nowadays and knowledge of it is always useful. Let’s discuss certain ways in which this task can be performed. 

Method 1 : Naive method + max() 

In this method, we simply iterate through the string and form a key in a dictionary of newly occurred element or if element is already occurred, we increase its value by 1. We find maximum occurring character by using max() on values. 

Python3




# Python 3 code to demonstrate
# Maximum frequency character in String
# naive method
 
# initializing string
test_str = "GeeksforGeeks"
 
# printing original string
print ("The original string is : " + test_str)
 
# using naive method to get
# Maximum frequency character in String
all_freq = {}
for i in test_str:
 if i in all_freq:
  all_freq[i] += 1
 else:
  all_freq[i] = 1
res = max(all_freq, key = all_freq.get)
 
# printing result
print ("The maximum of all characters in GeeksforGeeks is : " + str(res))


Output : 

The original string is : GeeksforGeeks
The maximum of all characters in GeeksforGeeks is : e

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2 : Using collections.Counter() + max() 

The most suggested method that could be used to find all occurrences is this method, this actually gets all element frequency and could also be used to print single element frequency if required. We find maximum occurring character by using max() on values. 

Python3




# Python 3 code to demonstrate
# Maximum frequency character in String
# collections.Counter() + max()
from collections import Counter
 
# initializing string
test_str = "GeeksforGeeks"
 
# printing original string
print ("The original string is : " + test_str)
 
# using collections.Counter() + max() to get
# Maximum frequency character in String
res = Counter(test_str)
res = max(res, key = res.get)
 
# printing result
print ("The maximum of all characters in GeeksforGeeks is : " + str(res))


Output : 

The original string is : GeeksforGeeks
The maximum of all characters in GeeksforGeeks is : e

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3 : Using a dictionary

  • First, the string “GeeksforGeeks” is initialized and stored in the variable test_str.
  • An empty dictionary called freq is created. This dictionary will be used to store the frequency of each character in the string.
  • The for loop iterates over each character in the test_str string. For each character, it checks if the character already exists in the freq dictionary. If it does, the frequency count is incremented by 1. If not, a new key is added to the dictionary with a frequency count of 1.
  • After the for loop completes, the max() function is used to find the key with the maximum value in the freq dictionary. The key parameter is passed to the max() function to specify that the key with the maximum value (i.e., the character with the highest frequency) should be returned.
  • Finally, the result is printed to the console as a string.

Python3




# initializing string
test_str = "GeeksforGeeks"
 
# printing original string
print ("The original string is : " + test_str)
 
# creating an empty dictionary
freq = {}
 
# counting frequency of each character
for char in test_str:
    if char in freq:
        freq[char] += 1
    else:
        freq[char] = 1
 
# finding the character with maximum frequency
max_char = max(freq, key=freq.get)
 
# printing result
print("The maximum of all characters in GeeksforGeeks is : " + str(max_char))


Output

The original string is : GeeksforGeeks
The maximum of all characters in GeeksforGeeks is : e

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using a generator expression:

This approach uses a generator expression with the max function to find the character with the maximum frequency. The lambda function returns the frequency count for each character in the string, and the max function returns the character with the maximum frequency. This approach may be less efficient for longer strings because it has to count the frequency of each character multiple times.

Python3




test_str = "GeeksforGeeks"
res = max(test_str, key=lambda x: test_str.count(x))
print(res)


Output

e

Time Complexity: O(n)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads