🧮 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:
the output should display how often each character occurs:
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)
-
Create a
HashMap<Character, Integer>
→ To store each character as a key and its count as the value. -
Convert the string into a character array
→ UsetoCharArray()to easily loop through characters. -
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.
-
-
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"
| Step | Character | Already in Map? | Action | Updated Count |
|---|---|---|---|---|
| 1 | M | ❌ | Add M=1 | 1 |
| 2 | y | ❌ | Add y=1 | 1 |
| 3 | (space) | ❌ | Add space=1 | 1 |
| 4 | n | ❌ | Add n=1 | 1 |
| 5 | a | ❌ | Add a=1 | 1 |
| 6 | m | ❌ | Add m=1 | 1 |
| 7 | e | ❌ | Add e=1 | 1 |
| 8 | (space) | ✅ | Increment | 2 |
| 9 | i | ❌ | Add i=1 | 1 |
| 10 | s | ❌ | Add s=1 | 1 |
| 11 | (space) | ✅ | Increment | 3 |
| 12 | V | ❌ | Add V=1 | 1 |
| 13 | i | ✅ | Increment | 2 |
| 14 | n | ✅ | Increment | 2 |
| 15 | o | ❌ | Add o=1 | 1 |
| 16 | d | ❌ | Add d=1 | 1 |
✅ Final Map contents printed in iteration order:
📘 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