Java Program to Find Duplicate Characters in a String

Strings are a sequence of characters. In programming, they are used to represent text. Sometimes, a string might contain characters that appear more than once, i.e., duplicate characters. In this article, we'll write a Java program to identify these duplicate characters in a given string. 

The Strategy

  • Create an empty map (e.g., HashMap) to store the frequency of each character in the string. 
  • Traverse through the string one character at a time. 
  • Update the frequency count of each character in the map. 
  • After storing the counts, traverse the map to identify characters with a frequency greater than one. 

Java Program to Find Duplicate Characters in a String

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

public class DuplicateCharacterFinder {

    public static void main(String[] args) {
        String text = "Java Guides Net";

        System.out.println("Duplicate characters in the string are:");
        findDuplicates(text);
    }

    public static void findDuplicates(String str) {
        Map<Character, Integer> charCountMap = new HashMap<>();

        for (char c : str.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }

        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.print(entry.getKey() + " ");
            }
        }
    }
}

Output:

Duplicate characters in the string are:
a e 

Step by Step Explanation: 

Setting Up the String: 
        String text = "Java Guides Net";
We've initialized a string text with the word "programming". Our goal is to identify duplicate characters in this word. 

Initializing the Character Count Map: 
        Map<Character, Integer> charCountMap = new HashMap<>();
We use a HashMap to store the frequency count of each character in the string. The character is the key and its count is the value. 

Populating the Map with Character Counts: 
        for (char c : str.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }
We convert the string to a character array and iterate through each character. For every character, we update its count in the charCountMap. The getOrDefault method is a nifty way to handle characters that are not yet in the map. 

Identifying and Printing Duplicate Characters:
        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.print(entry.getKey() + " ");
            }
        }
 After populating the map, we iterate through it. For each entry, if the value (i.e., character count) is greater than one, we print the character (i.e., the key).

Conclusion

Identifying duplicate characters in a string helps beginners grasp the concepts of string manipulation, iteration, and the importance of using data structures like Maps. This article provides a simple, yet effective, approach to tackle this problem using foundational Java constructs. As you continue your Java journey, understanding the value of data structures in problem-solving will become clearer. Remember, it's all about using the right tools for the right problems. Keep coding and exploring!

Comments