Algorithm and program to find out duplicate character in a String

🧮 Find Duplicate Characters in a String (Java)

🧩 What problem are we solving?

The goal is to analyze a given string and determine how many times each character appears — effectively identifying duplicate or repeating characters.

For example, given:

"My name is Vinod"

the output should display how often each character occurs:

Character : Count : 3 Character : a Count : 1 Character : s Count : 1 Character : d Count : 1 Character : e Count : 1 Character : V Count : 1 Character : y Count : 1 Character : i Count : 2 Character : M Count : 1 Character : m Count : 1 Character : n Count : 2 Character : o Count : 1

This problem is useful for understanding:

  • How to work with maps (HashMap)

  • How to iterate characters in a string

  • How to count frequencies of elements efficiently


⚙️ Algorithm (Step-by-Step)

  1. Create a HashMap<Character, Integer>
    → To store each character as a key and its count as the value.

  2. Convert the string into a character array
    → Use toCharArray() to easily loop through characters.

  3. Iterate through the character array
    → For each character:

    • If it’s already in the map → increment the count.

    • If not → add it with count = 1.

  4. Display the results
    → Print each key-value pair to show how many times each character occurs.


🧠 Example Walkthrough

Let’s trace through the string:
"My name is Vinod"

StepCharacterAlready in Map?ActionUpdated Count
1MAdd M=11
2yAdd y=11
3(space)Add space=11
4nAdd n=11
5aAdd a=11
6mAdd m=11
7eAdd e=11
8(space)Increment2
9iAdd i=11
10sAdd s=11
11(space)Increment3
12VAdd V=11
13iIncrement2
14nIncrement2
15oAdd o=11
16dAdd d=11

✅ Final Map contents printed in iteration order:

(space)=3, a=1, s=1, d=1, e=1, V=1, y=1, i=2, M=1, m=1, n=2, o=1

📘 Java Implementation

package com.vinod.test;

import java.util.HashMap;
import java.util.Map;

/**
 * FindDuplicateCharactorInString
 *
 * Problem:
 *   Count and identify duplicate characters in a given string.
 *
 * Algorithm:
 *   1. Create a HashMap to store each character and its frequency.
 *   2. Convert the input string into a char[] array.
 *   3. Iterate through the array:
 *        - If the character exists in the map, increment its count.
 *        - Otherwise, add it to the map with count = 1.
 *   4. Print all characters and their counts.
 *
 * Example:
 *   Input:  "My name is Vinod"
 *   Output:
 *     Character :   Count : 3
 *     Character : a Count : 1
 *     Character : s Count : 1
 *     Character : d Count : 1
 *     Character : e Count : 1
 *     Character : V Count : 1
 *     Character : y Count : 1
 *     Character : i Count : 2
 *     Character : M Count : 1
 *     Character : m Count : 1
 *     Character : n Count : 2
 *     Character : o Count : 1
 *
 * Time Complexity:
 *   O(n) – Each character is processed once.
 *
 * Space Complexity:
 *   O(k) – Where k is the number of unique characters.
 *
 * Author: Vinod Kariyathungal Kumaran
 */
public class FindDuplicateCharactorInString {

    public static void main(String[] args) {
        Map duplicateMap = new HashMap<>();

        String str = "My name is Vinod";
        char[] chrs = str.toCharArray();

        for (Character ch : chrs) {
            if (duplicateMap.containsKey(ch)) {
                duplicateMap.put(ch, duplicateMap.get(ch) + 1);
            } else {
                duplicateMap.put(ch, 1);
            }
        }

        duplicateMap.forEach((k, v) ->
            System.out.println("Character : " + k + " Count : " + v)
        );
    }
} 
 
Character :   Count : 3
Character : a Count : 1
Character : s Count : 1
Character : d Count : 1
Character : e Count : 1
Character : V Count : 1
Character : y Count : 1
Character : i Count : 2
Character : M Count : 1
Character : m Count : 1
Character : n Count : 2
Character : o Count : 1
 

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts