Java Program to Find Duplicate Characters in a String

Ramesh Fadatare
2 min readDec 13, 2024

--

Introduction

Finding duplicate characters in a string is a common task in text processing. It helps you understand how to manipulate strings and use data structures like maps in Java. This guide will walk you through writing a Java program that identifies and counts the duplicate characters in a given string.

Learn everything about Java: https://www.javaguides.net/

Problem Statement

Create a Java program that:

  • Prompts the user to enter a string.
  • Finds and displays the characters that appear more than once in the string.
  • Displays the count of each duplicate character.

Example:

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

Solution Steps

  1. Read the String: Use the Scanner class to take the string as input from the user.
  2. Store Character Counts: Use a HashMap to store each character of the string as keys and their counts as values.
  3. Identify Duplicate Characters: Iterate through the map to find characters with a count greater than 1.
  4. Display the Duplicates: Print the duplicate characters and their counts.
  5. Close Resources: Close the Scanner class object automatically using the try-resource statement.

Java Program

// Java Program to Find Duplicate Characters in a String

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

public class DuplicateCharacters {
public static void main(String[] args) {
// Step 1: Read the string from the user
try(Scanner scanner = new Scanner(System.in)){
System.out.print("Enter a string: ");
String input = scanner.nextLine();

// Step 2: Store character counts
Map<Character, Integer> charCountMap = new HashMap<>();
for (char ch : input.toCharArray()) {
charCountMap.put(ch, charCountMap.getOrDefault(ch, 0) + 1);
}

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

Explanation

Step 1: Read the String

  • The Scanner class is used to read a string input from the user. The nextLine() method captures the entire line as a string.

Step 2: Store Character Counts

  • A HashMap is used to store the characters as keys and their counts as values. The getOrDefault() method is used to initialize the count of each character.

Step 3: Identify and Display Duplicate Characters

  • The program iterates through the HashMap entries and checks for characters with a count greater than 1. These characters are duplicates and are printed along with their counts.

Output

Enter a string: programming
Duplicate characters:
r: 2
g: 2
m: 2

Conclusion

This Java program demonstrates how to find and display duplicate characters in a user-input string. It covers important concepts such as string manipulation, using maps to store character counts, and iterating through collections, making it a useful exercise for beginners learning Java programming.

Learn everything about Java: https://www.javaguides.net/

--

--

No responses yet