Java Program to Find Duplicate Characters in a String

Introduction

Finding duplicate characters in a string is a common task in programming, especially in text processing and data validation. This guide will show you how to create a Java program that identifies and displays duplicate characters in a given string.

Problem Statement

Create a Java program that:

  • Takes a string as input.
  • Finds and displays all duplicate characters in the string.

Example 1:

  • Input: "programming"
  • Output: Duplicate characters: r, g, m

Example 2:

  • Input: "hello"
  • Output: Duplicate characters: l

Solution Steps

  1. Prompt for Input: Use the Scanner class to read a string input from the user.
  2. Use a HashMap to Track Character Frequencies: Iterate through the string and store the frequency of each character in a HashMap.
  3. Identify Duplicate Characters: Traverse the HashMap to identify characters with a frequency greater than 1.
  4. Display the Duplicate Characters: Print the duplicate characters found in the string.

Java Program

Java Program to Find Duplicate Characters in a String

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

/**
 * Java Program to Find Duplicate Characters in a String
 * Author: https://www.javaguides.net/
 */
public class DuplicateCharactersInString {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Prompt the user for input
        System.out.print("Enter a string to find duplicate characters: ");
        String input = scanner.nextLine();

        // Step 2: Use a HashMap to track character frequencies
        Map<Character, Integer> charCountMap = new HashMap<>();

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

        // Step 3: Identify and display duplicate characters
        System.out.print("Duplicate characters: ");
        boolean hasDuplicates = false;
        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.print(entry.getKey() + " ");
                hasDuplicates = true;
            }
        }

        if (!hasDuplicates) {
            System.out.print("No duplicates found.");
        }

        System.out.println();
    }
}

Explanation

  • Input: The program prompts the user to enter a string.
  • Tracking Character Frequencies: The program uses a HashMap to count the occurrences of each character in the string. The getOrDefault method is used to simplify the process of incrementing the count.
  • Finding Duplicates: The program then iterates over the HashMap to identify characters with a frequency greater than 1, indicating that they are duplicates.
  • Output: The program prints the duplicate characters found in the string. If no duplicates are found, it prints a message indicating this.

Output Example

Example 1:

Enter a string to find duplicate characters: programming
Duplicate characters: r g m 

Example 2:

Enter a string to find duplicate characters: hello
Duplicate characters: l 

Example 3:

Enter a string to find duplicate characters: java
Duplicate characters: a 

Example 4:

Enter a string to find duplicate characters: abcdefg
Duplicate characters: No duplicates found.

Conclusion

This Java program effectively identifies duplicate characters in a string using a HashMap to track character frequencies. By counting the occurrences of each character and then filtering out those with a frequency greater than 1, the program efficiently finds and displays any duplicates. This approach is both simple and efficient, making it suitable for a wide range of text-processing tasks.

Comments