Python program to find the maximum frequency character in the string

Here, we will take input from the user and find the maximum frequency character in the string in Python.
Submitted by Shivang Yadav, on April 18, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Find the maximum frequency character in the string

We will take the string as input from the user and then find the maximum frequency character in the string.

Example

Input:
pythonprogramminglanguage

Output:
g

To find the most frequent character in the string, we will count the frequency of all characters in the string and then extract the character with maximum frequency.

To store characters and their frequencies, we will use a dictionary.

Python provides different methods to find the least frequent character.

Method 1: Using loop and max() method

We will loop over the array and find the frequency of occurrence of characters of the string. Then we will print the character with the maximum frequency.

Algorithm

  • Initialize: dictionary freq{}
  • Loop over the string
    • If the character exists, increase its frequency.
    • If the character does not exist in dict., add its frequency to 1.
  • Find the character with maximum frequency, using max() method.
  • Print the most frequent character.

Program to find the maximum frequency character in the string

# Python program to find the 
# maximum frequency character in the string

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the maximum frequency character of the string 
freq = {}
for i in myStr:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1
maxFreqChar = max(freq, key = freq.get)

# Printing values 
print("Entered String is ", myStr)
print(maxFreqChar , "is the maximum frequency character with frequency of " , freq[maxFreqChar])

Output:

Enter the string : pythonprogramminglanguage
Entered String is  pythonprogramminglanguage
g is the maximum frequency character with frequency of  4

Method 2: Using counter() method

Python provides a method counter() in its collections library which is used to count the frequency of values of a collection.

Then we will find the character with maximum frequency using the max() method.

Algorithm

  • Initialize: dictionary freq{}
  • Using collections.counter() method, find the frequency of each character.
  • Find the character with maximum frequency, using max() method.
  • Print the maximum frequency character.

Program

# Python program to find the 
# maximum frequency character of the string

import collections
# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the maximum frequency character of the string 
freq = freq = collections.Counter(myStr)
maxFreqChar = max(freq, key = freq.get)

# Printing values 
print("Entered String is ", myStr)
print(maxFreqChar , "is the maximum frequency character with frequency of " , freq[maxFreqChar])

Output:

Enter the string : pythonprogramminglanguage
Entered String is  pythonprogramminglanguage
g is the maximum frequency character with frequency of  4

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.