Find all Duplicate Characters in String in Python (original) (raw)

Last Updated : 31 Oct, 2025

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.

Using collections.Counter

Counter() function automatically counts how many times each character appears. Then, we can easily extract characters that occur more than once.

Python `

from collections import Counter

s = "GeeksforGeeks" d = Counter(s)

res = [c for c, cnt in d.items() if cnt > 1] print(res)

`

Output

['G', 'e', 'k', 's']

**Explanation:

Using Loop with Dictionary

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.

Python `

s = "GeeksforGeeks" d = {} res = []

for c in s: d[c] = d.get(c, 0) + 1

for c, cnt in d.items(): if cnt > 1: res.append(c)

print(res)

`

Output

['G', 'e', 'k', 's']

**Explanation:

Using defaultdict from collections

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.

Python `

from collections import defaultdict

s = "GeeksforGeeks" d = defaultdict(int)

for c in s: d[c] += 1

res = [c for c in d if d[c] > 1] print(res)

`

Output

['G', 'e', 'k', 's']

**Explanation:

Using set() and count()

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.

Python `

s = "GeeksforGeeks" res = []

for c in set(s): if s.count(c) > 1: res.append(c)

print(res)

`

Output

['k', 's', 'G', 'e']

**Explanation: