1. Introduction
In Java, we often encounter scenarios where we need to find duplicate characters in a given string. This exercise not only helps in understanding string manipulation but also introduces us to handling character frequency in a string using data structures like HashMap.
2. Program Steps
1. Initialize a String that needs to be checked for duplicate characters.
2. Create a HashMap to store each character of the string and its frequency.
3. Loop through each character of the string.
4. For each character, check if it is already present in the HashMap.
5. If it is present, increment its frequency, else add it to the HashMap with frequency 1.
6. After storing all characters in the HashMap, loop through the HashMap and print characters with a frequency greater than 1.
3. Code Program
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class FindDuplicateCharacters {
public static void main(String[] args) {
// Step 1: Initialize a String that needs to be checked for duplicate characters
String inputString = "programming";
// Step 2: Create a HashMap to store each character of the string and its frequency
Map<Character, Integer> charCountMap = new HashMap<>();
// Step 3: Loop through each character of the string
char[] strArray = inputString.toCharArray();
for (char c : strArray) {
// Step 4: For each character, check if it is already present in the HashMap
// Step 5: If it is present, increment its frequency, else add it to the HashMap with frequency 1
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
// Step 6: After storing all characters in the HashMap, loop through the HashMap and print characters with a frequency greater than 1
System.out.println("Duplicate Characters In " + inputString);
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
}
Output:
Duplicate Characters In programming r : 2 g : 2 m : 2
4. Step By Step Explanation
Step 1: We start by initializing the String named inputString that we will check for duplicate characters.
Step 2: We create a HashMap named charCountMap to store each character from the inputString and its frequency. The key of this map is the character, and the value is its frequency in the string.
Step 3: We convert the inputString into a character array strArray and loop through each character of this array.
Step 4 and 5: For each character, we check if it is already present in the charCountMap. If it is, we increment its frequency; otherwise, we add it to the HashMap with a frequency of 1 using the put method and getOrDefault method.
Step 6: After storing all the characters and their frequencies in the charCountMap, we loop through the HashMap using the entrySet method and print the characters which have a frequency greater than 1, indicating that they are duplicate characters in the given string.
This way, by using HashMap, we can efficiently find and print the duplicate characters in a string along with their frequencies.