Python program to find the most occurring character and its count

Given a string, write a python program to find the most occurrence character and its number of occurrences.

Examples:

Input : hello
Output : ('l', 2)

Input : geeksforgeeks
Output : ('e', 4)


We can solve this problem quickly in python using Counter() method. Simple Approach is to
1) Create a dictionary using Counter method having strings as keys and their frequencies as values.
2) Find the maximum occurrence of a character i.e. value and get the index of it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the most occurring
# character and its count
from collections import Counter
  
def find_most_occ_char(input):
  
    # now create dictionary using counter method
    # which will have strings as key and their 
    # frequencies as value
    wc = Counter(input)
  
    # Finding maximum occurrence of a character 
        # and get the index of it.
    s = max(wc.values())
    i = wc.values().index(s)
      
    print wc.items()[i]
  
# Driver program
if __name__ == "__main__":
    input = 'geeksforgeeks'
    find_most_occ_char(input)

chevron_right


Output:

('e', 4)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.