Program to find the most frequent character with its occurrence count using Python

by | Aug 10, 2021 | Python Programs

Home » Python » Python Programs » Program to find the most frequent character with its occurrence count using Python

Introduction

The task is to find the frequent occurred character in the given string with it occurrence number.

Program to find the most frequent character with its occurrence count using Python 

Program

from collections import Counter




def find(ip_char):

    count = Counter(ip_char)

# Most occured char

    max_occur = max(count.values())

    index = count.values().index(max_occur)   

    print(count.items()[index])

ip_char = 'alphabet'

find(ip_char)

Output

(‘a’, 2)

Explanation

In the above python code, we have transformed the given string into a dictionary using COUNTER() function. The COUNTER() function stores the data as key-value pair where keys are the characters and values as its number of occurrence. We extract the maximum occurred value and its index and print the output.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author