Java Program to check for Duplicate Characters
Program
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Scanner;
public class DuplicateCharactersCheck {
public void countDuplicateChar(String string){
Map<Character, Integer> countMap = new HashMap<Character, Integer>();
char[] characters = string.toCharArray();
for(Character ch:characters){
if(countMap.containsKey(ch)){
countMap.put(ch, countMap.get(ch)+1);
} else {
countMap.put(ch, 1);
}
}
Set<Character> keys = countMap.keySet();
for(Character ch:keys){
if(countMap.get(ch) > 1) {
System.out.println(ch+" "+countMap.get(ch));
}
else{
System.out.println("No Duplicate characters present");
}
}
}
public static void main(String[] args){
DuplicateCharactersCheck duplicateCharactersCheck = new DuplicateCharactersCheck();
System.out.println("Example String 1: www.oodlescoop.com");
duplicateCharactersCheck.countDuplicateChar("www.oodlescoop.com");
System.out.println("\nExample String 2: Engineering");
duplicateCharactersCheck.countDuplicateChar("Engineering");
Scanner reader = new Scanner(System.in);
System.out.println("Enter a string: ");
String enteredString = reader.next();
System.out.println("Duplicate Count for string : "+ enteredString +" is: ");
duplicateCharactersCheck.countDuplicateChar(enteredString);
}
}
This program identifies and counts duplicate characters in a given string using a HashMap
.
-
Input:
- The program takes predefined strings and user input to analyze duplicate characters.
-
Logic:
- HashMap is used to store characters and their frequencies:
- Key: A character from the string.
- Value: The count of occurrences of the character.
- HashMap is used to store characters and their frequencies:
-
Steps:
- Convert the input string to a character array using
toCharArray()
. - Loop through each character:
- If the character already exists in the
HashMap
, increment its value. - Otherwise, add the character to the
HashMap
with an initial count of1
.
- If the character already exists in the
- Convert the input string to a character array using
-
Check for Duplicates:
- Iterate through the
HashMap
:- If a character's count is greater than
1
, print the character and its frequency. - Otherwise, print that no duplicates are present.
- If a character's count is greater than
- Iterate through the
-
Output:
- For each string, duplicate characters and their counts are displayed.
- Example:
Example String 1: www.oodlescoop.com w 3 o 5 l 1 e 1 s 1 c 2 p 1 m 1
-
Usage of
Set
:- The
keySet()
method retrieves all unique characters (keys) in the map.
- The
-
Interactive Input:
- The program prompts the user to enter a string and displays the duplicate character count for the input.
Features:
- HashMap Efficiency:
- Provides quick lookups and updates for character counts.
- Character-by-Character Analysis:
- Ensures each character is processed only once.
- Flexibility:
- Works with any string input, including spaces and special characters.
Example Execution:
- Input:
Example String 2: Engineering
- Process:
- Counts:
e
: 3 timesn
: 3 timesi
: 2 timesg
: 2 times
- Output:
e 3 n 3 i 2 g 2
- Counts:
Use Case:
This program can be used for:
- Validating strings for duplicate characters.
- Data analysis in text processing.
- Debugging or identifying redundant characters in user input.
Output
$ javac DuplicateCharactersCheck.java
$ java DuplicateCharactersCheck
Example String 1: www.oodlescoop.com
c 2
w 3
. 2
o 5
Example String 2: Engineering
e 2
g 2
i 2
n 3
Enter a string:
Programming
Duplicate Count for string : Programming is:
r 2
g 2
m 2