Table of Contents
Introduction
The task is to find the frequent occurred character in the given string with it occurrence number.
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.
0 Comments