Java String Program · S15
Find Duplicate Characters in a String in Java
Count every character, then report the ones whose count is above one. Keeping the output in first-appearance order is the detail that separates a good answer.
Problem Statement
Write a method that returns every character that occurs more than once in a string, each reported once, in the order of its first appearance.
Spaces are ignored and the comparison is case-sensitive. If no character repeats, return an empty list.
Input/Output Examples
"committee"Output[m, t, e]Explanation: m, t and e each appear twice; c, o and i appear once."Java Developer"Output[a, v, e]Explanation: a and v appear twice and e three times; the space is skipped."abc"Output[]Explanation: every character is unique.Visual Explanation
"committee": every character with a total count above 1 is a duplicate (amber), reported once in first-appearance order.The method needs the full count before it can decide, because a character that looks unique early may appear again later. That is why the solution makes two passes: one to count and one to collect.
Approaches
Both approaches count first and collect second. They differ in the counting structure.
Approach 1: LinkedHashMap Counts (Recommended)
Idea: count characters in a LinkedHashMap, then keep the keys whose count is greater than 1.
- Skip spaces and call
counts.merge(c, 1, Integer::sum)for every other character. - Iterate over the map entries, which come back in first-insertion order.
- Add every key with a value above 1 to the result.
Time: O(n) · Space: O(k).
Approach 2: int[128] Array
Idea: for ASCII text, count in an array indexed by the character code, then walk the string again in order.
- Increase
counts[c]for every character. - Walk the string again; when a non-space character has a count above 1, add it.
- Set its count to 0 so it is reported only once.
Time: O(n) · Space: O(1). The array has a fixed 128 slots.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: LinkedHashMap Counts (Recommended)
Works for any character, including Unicode letters. k is the number of distinct characters.
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class DuplicateCharsMap {
// Counts in first-appearance order, then keeps the characters seen more than once
static List<Character> findDuplicates(String text) {
Map<Character, Integer> counts = new LinkedHashMap<>();
for (char c : text.toCharArray()) {
if (c != ' ') {
counts.merge(c, 1, Integer::sum);
}
}
List<Character> duplicates = new ArrayList<>();
for (Map.Entry<Character, Integer> e : counts.entrySet()) {
if (e.getValue() > 1) {
duplicates.add(e.getKey());
}
}
return duplicates;
}
public static void main(String[] args) {
String[] inputs = {"committee", "Java Developer", "abc"};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + findDuplicates(s));
}
}
}
"committee" -> [m, t, e] "Java Developer" -> [a, v, e] "abc" -> []
Time complexity: O(n) · Space complexity: O(k).
Approach 2: int[128] Array
Faster and lighter than a map, but limited to ASCII; a character such as é would be out of range.
import java.util.ArrayList;
import java.util.List;
public class DuplicateCharsArray {
// ASCII only: count with an int[128], then walk the string again to keep the order
static List<Character> findDuplicates(String text) {
int[] counts = new int[128];
for (char c : text.toCharArray()) {
counts[c]++;
}
List<Character> duplicates = new ArrayList<>();
for (char c : text.toCharArray()) {
if (c != ' ' && counts[c] > 1) {
duplicates.add(c);
counts[c] = 0; // report each character only once
}
}
return duplicates;
}
public static void main(String[] args) {
String[] inputs = {"committee", "Java Developer", "abc"};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + findDuplicates(s));
}
}
}
"committee" -> [m, t, e] "Java Developer" -> [a, v, e] "abc" -> []
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| LinkedHashMap Counts | O(n) | O(k) |
| int[128] Array | O(n) | O(1) |
Here n is the length of the string. Both approaches make two linear passes. The map grows with the number of distinct characters, while the array always has 128 slots.
Interview Tips and Recommended Approach
- Clarify the output order. First appearance needs a
LinkedHashMapor a second pass over the string; a plainHashMapgives no order. - Report each duplicate once. Printing inside the first loop would print
etwice for"committee". - Ask about case and spaces. Should
Jandjcount as the same character? - Avoid nested loops. Comparing every pair of characters works but costs
O(n²).
Recommended Approach: LinkedHashMap Counts
Why: it works for every character, keeps the natural first-appearance order and runs in linear time with simple code.