![]() |
VOOZH | about |
Given a string, the task is to find all characters that appear more than once in it. For example:
Input: "GeeksforGeeks"
Output: ['G', 'e', 'k', 's']
Below are multiple methods to find all duplicate characters in a string efficiently.
Counter() function automatically counts how many times each character appears. Then, we can easily extract characters that occur more than once.
['G', 'e', 'k', 's']
Explanation:
Here, for loop is used to find duplicate characters efficiently. First we count the occurrences of each character by iterating through the string and updating a dictionary. Then we loop through the dictionary to identify characters with a frequency greater than 1 and append them to the result list.
['G', 'e', 'k', 's']
Explanation:
This method is similar to the dictionary approach but uses defaultdict to automatically handle missing keys. It counts character occurrences efficiently without needing manual initialization.
['G', 'e', 'k', 's']
Explanation:
The count() method can be used to determine the frequency of each character in the string directly. While this approach is simple but it is less efficient for larger strings due to repeated traversals.
['k', 's', 'G', 'e']
Explanation: