Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs

Create the function and 2 variables: max and counter. Set the value of max to an empty string: "", and the value of counter to 0. Then iterate over the characters in the parameter string using a for loop. Use the in-built String function "count(substring)" to count the number of times the i'th characters appears in the string. Check if count(i) is greater than counter. If yes, set the value of max to i and the value of counter to count(i). Return the tuple max, counter.def most_occuring_char(string): max = "" counter = 0

for c in string: if string.count(c) > counter: max = c counter = count(c)

return max, counter The time complexity of the solution is O(n2) as the function iterates over all characters in the string and uses the in-built function "count(substring)" which iterates over the string for each character to count it's occurrences.

Answered by Volf A. Python tutor

1769 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a function that checks whether a number is prime or not.


Create a program that takes in two numbers and returns the highest of the two


Define a function that takes in the age of the user and adds it to an output sentence such as "You are ..... old".


Implement a fibonacci function which calculates the nth number of the fibonacci sequence.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo
Cookie Preferences