Introduction
Finding duplicate characters in a string is one of the most common Java programming problems for beginners. It helps developers understand important concepts such as strings, character arrays, loops, conditional statements, HashMap, and collections.
A duplicate character is a character that appears more than once in a string.
For example:
Input String:
programming
Duplicate Characters:
r
g
m
In the above string:
- r appears two times.
- g appears two times.
- m appears two times.
Java provides multiple ways to find duplicate characters in a string, such as:
- Using nested loops
- Using character arrays
- Using HashMap
- Using Java Streams
This program is commonly asked in Java interviews because it tests a developer’s understanding of string manipulation and data structures.
In this tutorial, you will learn how to write a Java program to find duplicate characters in a string, understand different approaches, analyze complexity, learn common mistakes, and prepare interview questions.
What are Duplicate Characters?
Duplicate characters are characters that occur more than one time in a given string.
Example:
String:
developer
Character Frequency:
d → 1
e → 3
v → 1
l → 1
o → 1
p → 1
r → 1
Duplicate Character:
e
The character e is displayed because it appears multiple times.
Syntax
The basic syntax to check duplicate characters:
for(int i = 0; i < text.length(); i++) {
for(int j = i + 1; j < text.length(); j++) {
if(text.charAt(i) == text.charAt(j)) {
System.out.println(text.charAt(i));
}
}
}
Java Duplicate Characters Program Using Nested Loop
public class DuplicateCharacters {
public static void main(String[] args) {
String text = "programming";
System.out.println("Duplicate Characters:");
for(int i = 0; i < text.length(); i++) {
for(int j = i + 1; j < text.length(); j++) {
if(text.charAt(i) == text.charAt(j)) {
System.out.println(text.charAt(i));
break;
}
}
}
}
}
Output
r
g
m
Explanation of the Program
Step 1: Create a String
String text = "programming";
The string contains characters that need to be checked.
Step 2: Use Outer Loop
for(int i = 0; i < text.length(); i++)
The outer loop selects each character one by one.
Example:
p
r
o
g
r
a
m
m
i
n
g
Step 3: Use Inner Loop
for(int j = i + 1; j < text.length(); j++)
The inner loop compares the selected character with the remaining characters.
Step 4: Compare Characters
if(text.charAt(i) == text.charAt(j))
If both characters are equal, it means the character is duplicated.
Java Duplicate Characters Program Using HashMap
Using HashMap is one of the most efficient approaches because it stores character frequency.
import java.util.HashMap;
import java.util.Map;
public class DuplicateCharacters {
public static void main(String[] args) {
String text = "programming";
HashMap map = new HashMap<>();
for(char ch : text.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
System.out.println("Duplicate Characters:");
for(Map.Entry entry : map.entrySet()) {
if(entry.getValue() > 1) {
System.out.println(entry.getKey());
}
}
}
}
Output
r
g
m
Explanation of HashMap Approach
Step 1: Convert String into Characters
text.toCharArray()
This converts the string into a character array.
Example:
programming
p r o g r a m m i n g
Step 2: Store Character Frequency
map.put(ch, map.getOrDefault(ch, 0) + 1);
Example:
p → 1
r → 2
g → 2
m → 2
Step 3: Find Characters with Count Greater Than One
if(entry.getValue() > 1)
If the frequency is greater than one, the character is duplicated.
Java Duplicate Characters Program Using User Input
import java.util.Scanner;
import java.util.HashMap;
public class DuplicateCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String text = scanner.nextLine();
HashMap map = new HashMap<>();
for(char ch : text.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
System.out.println("Duplicate Characters:");
for(char ch : map.keySet()) {
if(map.get(ch) > 1) {
System.out.println(ch);
}
}
scanner.close();
}
}
Output
Enter a string:
Duplicate Characters:
a
g
r
m
Java Duplicate Characters Without HashMap
Another approach is using a character array.
public class DuplicateCharacters {
public static void main(String[] args) {
String text = "success";
char[] characters = text.toCharArray();
System.out.println("Duplicate Characters:");
for(int i = 0; i < characters.length; i++) {
int count = 1;
for(int j = i + 1; j < characters.length; j++) {
if(characters[i] == characters[j]) {
count++;
characters[j] = '0';
}
}
if(count > 1 && characters[i] != '0') {
System.out.println(characters[i]);
}
}
}
}
Output
s
c
Algorithm
- Start the program.
- Read a string.
- Store each character.
- Compare characters with each other.
- Check whether any character appears more than once.
- Display duplicate characters.
- End the program.
Flowchart
Start
|
|
Read String
|
|
Select Character
|
|
Compare With Remaining Characters
|
|
Character Found Again?
|
Yes No
| |
Print Continue
Duplicate
|
Next Character
|
End
Time Complexity
Using Nested Loop
The program compares every character with other characters.
Time complexity:
O(n²)
where n is the length of the string.
Using HashMap
HashMap stores and retrieves characters efficiently.
Time complexity:
O(n)
Space Complexity
Nested Loop Approach
O(1)
No extra data structure is used.
HashMap Approach
O(n)
Extra memory is required to store character frequencies.
Advantages of Finding Duplicate Characters
- Improves understanding of strings.
- Helps learn frequency counting.
- Useful for data validation.
- Important interview programming problem.
- Helps understand HashMap usage.
Disadvantages
- Nested loop approach is slower for large strings.
- Additional memory is required for HashMap.
- Handling case sensitivity requires extra logic.
Common Mistakes
1. Printing Duplicate Characters Multiple Times
Incorrect output:
r
r
g
g
m
m
Use a HashMap or break statement to avoid repetition.
2. Ignoring Uppercase and Lowercase Difference
Example:
Java
J
j
Java treats uppercase and lowercase characters differently.
3. Forgetting to Convert String Correctly
Incorrect:
text[i]
Correct:
text.charAt(i)
4. Not Handling Empty Strings
Example:
Input:
""
Output
5. Modifying Original String Incorrectly
Strings in Java are immutable, so use StringBuilder or character arrays for modifications.
Real-Life Applications
Finding duplicate characters is used in:
- Data validation systems
- Text analysis software
- Password checking systems
- Duplicate data detection
- Search engines
- Natural language processing
Examples:
- Detecting repeated words.
- Checking duplicate values.
- Analyzing text patterns
Summary
The Java Duplicate Characters Program is an important string-based programming problem that helps developers understand character frequency counting and data structures.
There are multiple ways to find duplicate characters in Java. The simple nested loop approach is easy for beginners, while the HashMap approach provides better performance for large strings.
Understanding this program helps build a strong foundation for advanced string problems such as character frequency count, anagram checking, duplicate words detection, and palindrome programs.