How to find maximum occurring character in a given string?

Example 1:- 
string = "RAMAN"
empty_dictionary = {}
for each_character in string:
    if(empty_dictionary.get(each_character)):
        value = empty_dictionary[each_character]
        value = value + 1
        empty_dictionary[each_character] = value
    else:
        empty_dictionary[each_character] = 1
max_repeat_character = max(empty_dictionary, key = empty_dictionary.get)
print("Maximum repeating character is",max_repeat_character)
Output:-

Maximum repeating character is A

Limitations of Example 1:

Example 1 will run only running for the same case letter. All the string characters should be in the same case uppercase or lowercase letter. If we mix uppercase and lowercase letters then the above will not give proper results.

Example 2:-
string = "RaMaN"
string = string.lower()
empty_dictionary = {}
for each_character in string:
    if(empty_dictionary.get(each_character)):
        value = empty_dictionary[each_character]
        value = value + 1
        empty_dictionary[each_character] = value
    else:
        empty_dictionary[each_character] = 1
max_repeat_character = max(empty_dictionary, key = empty_dictionary.get)
print("Maximum repeating character is",max_repeat_character)
Output :-
Maximum repeating character is a

In example 2, the program is a character case-independent means the program will count upper case letters as well as lower case letters.

Excercise :
Write a program to count the frequency/occuring of each element in python.
Send your solution at "contribute.articles@tech-bloggers.in"

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock