Python Program to Find Duplicate Characters in String
duplicates = {char: count for char, count in collections.Counter(string).items() if count > 1}.Examples
How to Think About It
Algorithm
Code
from collections import Counter def find_duplicates(s): counts = Counter(s) duplicates = {char: count for char, count in counts.items() if count > 1} return duplicates input_str = "programming" print(find_duplicates(input_str))
Dry Run
Let's trace the string 'programming' through the code to find duplicates.
Count characters
Counter counts each character: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}
Filter duplicates
Select characters with count > 1: {'r': 2, 'g': 2, 'm': 2}
Return duplicates
Return {'r': 2, 'g': 2, 'm': 2}
| Character | Count | Is Duplicate? |
|---|---|---|
| p | 1 | No |
| r | 2 | Yes |
| o | 1 | No |
| g | 2 | Yes |
| a | 1 | No |
| m | 2 | Yes |
| i | 1 | No |
| n | 1 | No |
Why This Works
Step 1: Counting characters
The Counter counts how many times each character appears in the string.
Step 2: Filtering duplicates
We keep only characters with counts greater than one because those are duplicates.
Step 3: Returning result
The program returns a dictionary showing duplicate characters and their counts.
Alternative Approaches
def find_duplicates_set(s): seen = set() duplicates = set() for char in s: if char in seen: duplicates.add(char) else: seen.add(char) return {char: s.count(char) for char in duplicates} print(find_duplicates_set('programming'))
def find_duplicates_manual(s): counts = {} for char in s: counts[char] = counts.get(char, 0) + 1 return {char: count for char, count in counts.items() if count > 1} print(find_duplicates_manual('programming'))
Complexity: O(n) time, O(n) space
Time Complexity
Counting characters requires one pass through the string, so it takes O(n) time where n is string length.
Space Complexity
We store counts for each unique character, which can be up to O(n) in the worst case.
Which Approach is Fastest?
Using collections.Counter is fastest and most readable; manual counting is similar but more verbose; set-based detection is simpler but less efficient for large strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| collections.Counter | O(n) | O(n) | Fast and clean counting |
| Manual dictionary count | O(n) | O(n) | No imports, clear logic |
| Set detection with count | O(n^2) | O(n) | Simple code, small strings |
collections.Counter for a quick and clean way to count characters.