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

1265 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What would output this code? print(hello world)


Create a program that checks for syntactical errors in an email address.


Write a program that computes the sum of all numbers up to a input number n


Generate an array of integers from 0 to 99 and split it into two smaller arrays. Each smaller array will contain half of the original.


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–2024

Terms & Conditions|Privacy Policy