Skip to content

Instantly share code, notes, and snippets.

@bharathmuddada
Last active April 12, 2021 09:49
Show Gist options
  • Select an option

  • Save bharathmuddada/d40002d4c6c9cefe2d3ddeb1e2f5324d to your computer and use it in GitHub Desktop.

Select an option

Save bharathmuddada/d40002d4c6c9cefe2d3ddeb1e2f5324d to your computer and use it in GitHub Desktop.
How to count the number of repeated characters in a string in Python
sentence = "This is a common interview question"
def most_repeated_char(sentence):
character_freq = {}
for char in sentence:
if char in character_freq:
character_freq[char] += 1
else:
character_freq[char] = 1
char_freq_sorted = sorted(character_freq.items(), key=lambda keyval: keyval[1], reverse=True)
return char_freq_sorted[0]
print(most_repeated_char(sentence))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment