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.

VA
Answered by Volf A. Python tutor

1916 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Demonstrate a recursive solution to calculate the factorial of a number


Which four data types are used in Python? Can you give an example of each?


Write a recursive function to find the factorial of a number n.


Create an rock, paper, scissors game. The user should input one option, and the computer should play randomly.


We're here to help

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

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences