Java Program to Count Duplicate Characters in a String

Introduction

Counting duplicate characters in a string is a common text-processing task. In this guide, we'll write a Java program that takes a string as input and counts the number of duplicate characters.

Problem Statement

Create a Java program that:

  • Takes a string input from the user.
  • Counts the number of characters that appear more than once in the string.
  • Displays the duplicate characters and their counts.

Example 1:

  • Input: "programming"
  • Output: {r=2, g=2, m=2}

Example 2:

  • Input: "hello"
  • Output: {l=2}

Example 3:

  • Input: "abcd"
  • Output: {} (No duplicates)

Solution Steps

  1. Input String: Accept a string input from the user.
  2. Normalize the String: Convert the string to lowercase to make the comparison case-insensitive.
  3. Count Character Frequencies: Use a HashMap to store the frequency of each character in the string.
  4. Identify Duplicates: Traverse the map to identify characters that have a frequency greater than 1.
  5. Output the Result: Display the duplicate characters and their counts.

Java Program

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

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

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

        // Step 1: Input String
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Step 2: Normalize the String
        input = input.toLowerCase();

        // Step 3: Count Character Frequencies
        Map<Character, Integer> charCountMap = new HashMap<>();
        for (char c : input.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }

        // Step 4: Identify Duplicates
        Map<Character, Integer> duplicates = new HashMap<>();
        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                duplicates.put(entry.getKey(), entry.getValue());
            }
        }

        // Step 5: Output the Result
        System.out.println("Duplicate Characters: " + duplicates);
    }
}

Explanation

Step 1: Input String

  • The program prompts the user to enter a string.

Step 2: Normalize the String

  • The string is converted to lowercase to ensure that character comparisons are case-insensitive.

Step 3: Count Character Frequencies

  • A HashMap is used to store the frequency of each character in the string. As the string is iterated, each character's count is updated in the map.

Step 4: Identify Duplicates

  • The program iterates over the HashMap to identify characters that have a frequency greater than 1. These characters are stored in another HashMap called duplicates.

Step 5: Output the Result

  • The program prints the duplicate characters and their counts.

Output Examples

Example 1:

Enter a string: programming
Duplicate Characters: {r=2, g=2, m=2}

Example 2:

Enter a string: hello
Duplicate Characters: {l=2}

Example 3:

Enter a string: abcd
Duplicate Characters: {}

Conclusion

This Java program efficiently counts and displays duplicate characters in a string. By using a HashMap to track the frequency of each character, the program can easily identify and report characters that appear more than once. This method is straightforward and can be easily modified to suit various string processing needs.

Comments