Python Mastery with Mosh Exercise; Most repeating character in a sentence.
September 07, 2020
Write a function that’s returns the first most repeating character in a sentence in Python.
For ex.: “aabcddds” -> (“d”, 3)
def most_char_frequency(sentence):
char_frequency = {}
for char in sentence:
if(char in char_frequency):
char_frequency[char] += 1
else:
char_frequency[char] = 1
char_frequency_sorted = sorted(
char_frequency.items(),
key=lambda kv: kv[1],
reverse=True)
return char_frequency_sorted[0]
sentence = "This is a common interview questions"
print(most_char_frequency(sentence))
Written by Ernest Saidu Kamara, a software engineer born in Sierra Leone and raised in Norway - Follow me on my social network. 👇🏿