🎓 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
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
- Prompt for Input: Use the
Scannerclass to read a string input from the user. - Use a
HashMapto Track Character Frequencies: Iterate through the string and store the frequency of each character in aHashMap. - Identify Duplicate Characters: Traverse the
HashMapto identify characters with a frequency greater than 1. - 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
HashMapto count the occurrences of each character in the string. ThegetOrDefaultmethod is used to simplify the process of incrementing the count. - Finding Duplicates: The program then iterates over the
HashMapto 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.
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