Find the most occurring character and its count in Python

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Python program, we will have a look at How to find the most occurring character and its count in Python programming language.

Python program to find the most occurring character and its count

from collections import Counter 
def find_most_occ_char(input): 
 wc = Counter(input) 
 s = max(wc.values()) 
 i = wc.values().index(s) 
 print wc.items()[i] 
if __name__ == "__main__": 
 input = 'codeatglance'
 find_most_occ_char(input) 

Output: (‘a’, 2)

In the above code, we have passed the characters as “codeatglance” and therefore we get the output as ‘a’ as the most occurring character in the string.

Leave a Comment

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