Python program to find the most occurring character and its count
Last Updated : 15 Mar, 2023
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)
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)
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
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.
Similar Reads
Python program to find occurrence to each character in given string Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let's discuss a few of them. Method #1: Using set() + count() Iterate over the set converted string and get the count
5 min read
Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is
2 min read
Python program to equal character frequencies Given a String, ensure it has equal character frequencies, if not, equate by adding required characters. Input : test_str = 'geeksforgeeks' Output : geeksforgeeksggkkssfffooorrr Explanation : Maximum characters are 4 of 'e'. Other character are appended of frequency 4 - (count of chars). Input : tes
2 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read