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.
# 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) |
Output:
('e', 4)
Recommended Posts:
- Python Program to print digit pattern
- Python | Extract key-value of dictionary in variables
- Python program to convert time from 12 hour to 24 hour format
- Google Chrome Dino Bot using Image Recognition | Python
- Python | Use of __slots__
- Python - Summation of float string list
- Python | Animated Banner showing 'GeeksForGeeks'
- Python | Segregate list elements by Suffix
- Python Program for Selection Sort
- Python Program for Bubble Sort
- Python Program for Insertion Sort
- Python Program for Heap Sort
- Python Program for Counting Sort
- Python Program for Radix Sort
- Python Program for n-th Fibonacci number
- Python Program for Counting Sort
- Python Program for ShellSort
- Python Program for Longest Common Subsequence
- Python Program for Binary Search (Recursive and Iterative)
- Python Program for 0-1 Knapsack Problem
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.

