Last active
April 12, 2021 09:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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