Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python program to find the most occurring character and its count
Next article icon

Python program to find the most occurring character and its count

Last Updated : 15 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
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. 


Next Article
Python program to find the most occurring character and its count

A

AFZAL ANSARI
Improve
Article Tags :
  • Python
  • Python Programs
  • DSA
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences