Open In App

Maximum Frequency Character in String – Python

Last Updated : 28 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.Counter

Counter class from the collections module is an easy way to count the occurrences of each character in a string. It returns a dictionary-like object where keys are the characters and values are their frequencies.

from collections import Counter

s = "hello world"

# Count the frequency of characters
frequency = Counter(s)

# Get the character with the maximum frequency
max_char = max(frequency, key=frequency.get)

print(max_char)

Output
l

Explanation

  • Counter(s) creates a dictionary where the keys are characters and the values are the counts of those characters in the string s.
  • max(frequency, key=frequency.get) finds the character with the highest frequency by comparing the values (counts) in the dictionary. The result is stored in max_char and printed.

Using dict.get() with max()

This method involves creating a dictionary to store the frequency of each character and then using the max() function to find the character with the highest frequency. The dict.get() method helps in counting occurrences efficiently.

s = "hello world"

# Create a dictionary to store character frequencies
freq = {}

# Count the frequency of each character
for char in s:
    freq[char] = freq.get(char, 0) + 1

# Get the character with the maximum frequency
max_char = max(freq, key=freq.get)

print(max_char)

Output
l

Explanation

  • We loop through each character in the string and use freq.get(char, 0) + 1 to update the frequency of each character.
  • max(freq, key=freq.get) finds the key (character) with the highest frequency by comparing the values (counts) in the dictionary .

Using str.count()

str.count() method counts the occurrences of a specific character in the string. By iterating over each unique character and using this method, we can find the one with the maximum frequency.

s = "hello world"

# Initialize variables for tracking max frequency and character
max_char = ''
max_count = 0

# Loop through each unique character
for char in set(s):
    count = s.count(char)
    if count > max_count:
        max_count = count
        max_char = char

print(max_char)

Output
l

Explanation

  • We loop through each unique character in the string using set(s) to avoid duplicates.
  • s.count(char) is used to calculate the frequency of each character. If a character’s count is greater than the current max_count, we update max_count and max_char accordingly.

Using sorted() with key

sorted() function can be used to sort the characters based on their frequency. By passing a custom sorting key like s.count(char), we can sort the characters in descending order of frequency and select the first one.

s = "hello world"

# Sort the string and find the character with the maximum frequency
max_char = sorted(set(s), key=lambda char: s.count(char), reverse=True)[0]

print(max_char)

Output
l

Explanation

  • sorted(set(s), key=lambda char: s.count(char), reverse=True) sorts the unique characters from the string based on their frequency in descending order.
  • The [0] index retrieves the character with the highest frequency, which is then printed.


Practice Tags :

Similar Reads

three90RightbarBannerImg