🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Input String: Accept a string input from the user.
- Normalize the String: Convert the string to lowercase to make the comparison case-insensitive.
- Count Character Frequencies: Use a
HashMapto store the frequency of each character in the string. - Identify Duplicates: Traverse the map to identify characters that have a frequency greater than 1.
- 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
HashMapis 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
HashMapto identify characters that have a frequency greater than 1. These characters are stored in anotherHashMapcalledduplicates.
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment