Python Program to find Maximum frequency character in String
In this tutorial, we will learn to find the max frequent character in the string. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. The frequency of a character is the total number of times that character occurs in the given string. We have to find and print the character which occurs the most in the string in our program.
Look at the examples to understand the input and output format.
Input: "bcbsvbsvbwberb"
Output: b
Input: "Studytonight"
Output: t
Approach
To execute this task, we can follow multiple approaches, some are discussed below:
- By using keys in the dictionary and max() method
- By using Counter() and max() method
We will discuss all three approaches in detail separately.
Approach 1: Keys of dictionary and max() method
In this approach, we will iterate through the string and form keys of the characters which occur in the string. The keys will have the value of how many times that character has occurred in the string. Then, using the max() method, we can find the key with the maximum count value, which will be then used to print the result.
The max() method returns the item with the highest value.
Algorithm
Step 1- Define a string with characters
Step 2- Declare an empty dictionary
Step 3- Run a loop to iterate through the string
Step 4- Add the character in the dictionary and keep a count of the frequency
Step 5- To get the maximum count use max() and store the value returned by it in a variable
Step 6- Print the variable which has the lowest count as the result
Python Program 1
In this program, we have defined a string with certain values and printed the least frequent character in that string using a dictionary char_freq, storing the characters and their occurrences as the key and its value. We can get the Key using the get() method.
string= "mississippis"
print(string)
char_freq={}
for i in string:
if i in char_freq:
char_freq[i]=char_freq[i]+1
else:
char_freq[i] = 1
result= max(char_freq, key = char_freq.get)
print("Most frequent character: ",result)
mississippis
Most frequent character: s
Approach 2: Counter() and max()
The Counter() method can be used to get the frequency of every character in the string then we can use the max() method to get the maximum count value.
The counter is a sub-class that counts all the hashable objects.
Algorithm
Step 1- Import Counter
Step 2- Declare a string with characters
Step 3- Print string
Step 4- Call the Counter() and pass the string
Step 5- To get the maximum count use max() and store the value returned by it in a variable
Step 6- Print the variable as the result
Python Program 2
In this program, to use Counter(), we have to import it from the collections class in our program. Since Counter works for hashable objects they are accessed using a key. We can get the Key using the get() method.
from collections import Counter
string= "pppppppghhhijeuupffe"
print(string)
result= Counter(string)
result= max(result, key=result.get)
print("Most frequent character: ",result)
pppppppghhhijeuupffe
Most frequent character: p
Conclusion
In this tutorial, we have seen two approaches to get the most frequent character from the string. The first approach was to find the maximum count using keys of a dictionary and using the key to print the most frequent character. The second approach was to use the Counter() method to get the total count for all characters. Using the max() method, we can get the maximum count value.