0
0
PythonProgramBeginner · 2 min read

Python Program to Find Duplicate Characters in String

You can find duplicate characters in a string by using a dictionary to count each character and then selecting those with counts greater than one, for example: duplicates = {char: count for char, count in collections.Counter(string).items() if count > 1}.
📋

Examples

Inputhello
Output{'l': 2}
Inputprogramming
Output{'r': 2, 'g': 2, 'm': 2}
Inputabc
Output{}
🧠

How to Think About It

To find duplicate characters, first count how many times each character appears in the string. Then, pick only those characters that appear more than once. This way, you identify duplicates easily by comparing counts.
📐

Algorithm

1
Get the input string.
2
Create a count of each character in the string.
3
Check which characters have a count greater than one.
4
Collect these characters and their counts as duplicates.
5
Return or print the duplicates.
💻

Code

python
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))
Output
{'r': 2, 'g': 2, 'm': 2}
🔍

Dry Run

Let's trace the string 'programming' through the code to find duplicates.

1

Count characters

Counter counts each character: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}

2

Filter duplicates

Select characters with count > 1: {'r': 2, 'g': 2, 'm': 2}

3

Return duplicates

Return {'r': 2, 'g': 2, 'm': 2}

CharacterCountIs Duplicate?
p1No
r2Yes
o1No
g2Yes
a1No
m2Yes
i1No
n1No
💡

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

Using a set to track seen and duplicates
python
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'))
This method uses sets and counts duplicates after detection; it is simpler but less efficient for long strings.
Using a dictionary to count manually
python
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'))
This method manually counts characters without importing modules; it is clear but more code.

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.

ApproachTimeSpaceBest For
collections.CounterO(n)O(n)Fast and clean counting
Manual dictionary countO(n)O(n)No imports, clear logic
Set detection with countO(n^2)O(n)Simple code, small strings
💡
Use Python's collections.Counter for a quick and clean way to count characters.
⚠️
Beginners often forget to check if the count is greater than one, returning all characters instead of duplicates only.