Open In App

Python program to find the most occurring character and its count

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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)

Time Complexity: O(N) where N is the length of the input string
Auxiliary Space: O(1)

One more approach to find the most occurring character and its count in python3

Python3




# python code to find most occurring character in a string and its count
string1="geeksforgeeks"
list1=[]
list2=[]
for i in string1:
  if i not in list1:
    list1.append(i)#appending unique characters of string
    list2.append(string1.count(i))
#finding maximum in the list
occ=max(list2)
ele=list1[list2.index(occ)]
print(ele,occ)
    


Output

e 4

Time Complexity: O(N)
Auxiliary Space: O(N)

Approach : Using operator.countOf() and index() methods

Python3




# python code to find most occurring character in a string and its count
string1="geeksforgeeks"
list1=[]
list2=[]
for i in string1:
  if i not in list1:
    list1.append(i)#appending unique characters of string
    import operator
    list2.append(operator.countOf(string1,i))
#finding maximum in the list
occ=max(list2)
ele=list1[list2.index(occ)]
print(ele,occ)


Output

e 4

Time Complexity: O(N)
Auxiliary Space: O(N)

Approach: Using lambda function 

This new approach uses a lambda function to count the occurrences of each character in the string, and then creates a list of tuples containing the character and its count. Finally, it finds the tuple with the maximum count and prints its character and counts.

Python3




# python code to find most occurring character in a string and its count
string1 = "geeksforgeeks"
 
# use a lambda function to count occurrences of each character in the string
count_char = lambda c: string1.count(c)
 
# create a list of tuples containing the character and its count
char_counts = [(c, count_char(c)) for c in set(string1)]
 
# find the tuple with the maximum count and print its character and count
most_common = max(char_counts, key=lambda x: x[1])
print(most_common[0], most_common[1])
 
# Contributed by adityasha4x71


Output

e 4

Time Complexity: O(N*N), due to count() function used in the lambda

Auxiliary Space: O(N), for creating a list of tuples containing the character and its count. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads