The Wayback Machine - https://web.archive.org/web/20250214145420/https://www.geeksforgeeks.org/python-maximum-frequency-character-in-string/
Open In App

Python – Maximum frequency character in String

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

Maximum frequency character in String means finding the character that appears the most number of times within a given string.

Using collection.Counter

collections.Counter class is used to count the occurrences of each element in an iterable. It returns a dictionary-like object where keys are the elements, and values are the counts of those elements.

Python
from collections import Counter

# Input string
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)counts the frequency of each character in the string s, returning a dictionary-like object with characters as keys and their counts as values.
  • max(frequency, key=frequency.get) function finds the character with the highest frequency by comparing the counts, and the result is stored in max_char and printed.

Using dict.get() with max()

The combination of dict.get() with max() allows you to find the key in a dictionary that has the maximum value. This method is often used to identify elements with the highest frequency or significance based on their values.

Python
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

  • freq.get(char, 0) + 1 increments the frequency count of each character in the string s, with get() providing a default value of 0 if the character is not already in the dictionary.
  • max(freq, key=freq.get) identifies the character with the highest frequency by using the values in the dictionary as the criteria for comparison, and the result is stored in max_char and printed.

Using str.count()

str.count() method in Python counts the occurrences of a specified substring in a string, making it useful for determining the frequency of individual characters or words within a string.

Python
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

  • s.count(char) method calculates the frequency of each unique character in the string s, iterating over the unique characters using set(s) to avoid redundant checks.
  • If a character’s count is greater than the current max_count, the max_count and max_char are updated, storing the character with the highest frequency in max_char, which is then printed.

Using sorted() with key

Using sorted() with the key parameter allows you to sort elements based on a custom criterion, such as frequency in a string.

Python
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() function sorts the unique characters from set(s) based on their frequency in the string s, determined using s.count(char) as the sorting key. The reverse=True parameter ensures the sorting is in descending order of frequency.
  • The [0] index retrieves the first character from the sorted list, which has the highest frequency, and this character is stored in max_char and printed.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg