Find Duplicate Characters in a String in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

void findDup(char *s) {
    int a[256] = {0};
    while (*s) a[*s++]++;
    for (int i = 0; i < 256; i++)
        if (a[i] > 1) printf("%c: %d\n", i, a[i]);
}

C Output

r: 2  
g: 2  
m: 2


C++ Program

void findDup(string s) {
    int a[256] = {0};
    for (char c : s) a[c]++;
    for (int i = 0; i < 256; i++)
        if (a[i] > 1) cout << (char)i << ": " << a[i] << "\n";
}

C++ Output

s: 3  
c: 2


JAVA Program

void findDup(String s) {
    int[] a = new int[256];
    for (char c : s.toCharArray()) a[c]++;
    for (int i = 0; i < 256; i++)
        if (a[i] > 1) System.out.println((char)i + ": " + a[i]);
}

JAVA Output

a: 3  
n: 2


Python Program

def find_dup(s):
    from collections import Counter
    for c, count in Counter(s).items():
        if count > 1:
            print(f"{c}: {count}")

Python Output

e: 2  
n: 3  
g: 2  
i: 2


In-Depth Explanation
Example
Consider the string "programming". We need to determine which characters occur more than once.
Steps:

Travel through each character.

Count the occurrences via a map or array.

Print characters with count > 1.

In "programming", characters r, g, and m occur more than once, so they are printed along with their frequency.

Real-Life Analogy
Consider a class roll call. If there is more than one of the same name, that's a duplicate — perhaps an error or an actual twin! In a string, duplicates can assist with pattern recognition, errors, or repetitions.

Another analogy: Picture examining voter ID registrations — if a name or ID shows up more than once, it's marked for scrutiny. That's duplication detection in action.

Why It Matters
Finding repeating characters enhances your knowledge of:

Frequency counting

Hashing and arrays

String traversal efficiently

It also introduces you to the concept of compression, error detection, and pattern matching, which are essential in data validation, AI preprocessing, and string processing operations.

It's also applicable when addressing issues such as:

Verifying anagrams

Identifying the first non-repeating character

Data deduplication

What You Learn from This
How to map characters to frequency using arrays or hash maps

Maintaining proper handling of both lowercase and uppercase (if case-sensitive)

Working with string traversal methods

Applying reasoning to real-world data problems such as logs, names, inputs, etc.

This is a real-world problem that solidifies your foundation in arrays, strings, hashing, and condition checking.

Interview Relevance and Real Projects
This is a extremely common interview question for beginners and intermediate programmers. It tests whether you are able to:

Traverse and process a string

Use memory-efficient counting

Write clean, readable loop logic

You can be asked follow-ups such as:

Print only the first repeating character

Process case-insensitive strings

Return the duplicate characters rather than printing

In actual projects, it's employed in:

Input validation systems

User name filtering

Log analysis and parsing

AI/NLP preprocessing where duplicates impact word processing

SEO-Optimized Explanation
The problem of finding duplicate characters in a string using C, C++, Java, or Python is an everyday challenge that enables developers to grasp string manipulation, frequency counting, and loop management. Through a count of how many times every character occurs and the selection of those whose frequency is more than one, programmers develop critical skills in solving problems in everyday life such as finding errors, checking input, working with logs, and text data analysis. This code exercise enhances your understanding of arrays, hash maps, and character manipulation, making it perfect for beginners practicing for interviews, data structure exercises, and logic building for software projects.