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

1899 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

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


What is the difference between a float and an integer variable type?


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


Create an algorithm that can be used as a guessing game. Make sure to import random at the start.


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