Find First Repeated Character in a String in Java
Problem
The first repeated character is the earliest character, reading left to right, whose second occurrence is found before any other character's second occurrence.
Given a string, find the first character that has already appeared earlier in the string.
Java Program
import java.util.HashSet;
import java.util.Set;
public class FirstRepeatedCharacter {
public static void main(String[] args) {
String str = "programming";
Set<Character> seen = new HashSet<>();
for (char c : str.toCharArray()) {
if (seen.contains(c)) {
System.out.println("First repeated character: " + c);
break; // found the first repeat, no need to scan further
}
seen.add(c);
}
}
}Output
Core Logic
Tracking every character already seen in a set, and stopping the instant a character shows up that's already in it, finds the first repeat directly — no need to count anything.
- 1A
HashSet<Character>namedseenstarts empty. - 2For each character,
seen.contains(c)checks whether it has already been added. - 3The first time this check succeeds, that character is the answer — it's printed immediately and the loop
breaks. - 4If the check fails, the character is new so far, and
seen.add(c)records it before moving on.
"programming", 'p', 'r', 'o', and 'g' are each added as new — then the second 'r' at index 4 is found already in seen, so 'r' is reported.Key Point: This finds the earliest point in the string where a repeat happens, which isn't always the same as the character with the earliest first appearance among all duplicates — it's specifically about which second occurrence comes first.
Why: In the worst case (no repeats at all) every character gets added to the set, so the set can grow to hold up to n entries.
Key Concepts
Approach 2: Manual Boolean Array
public class FirstRepeatedCharacterManual {
public static void main(String[] args) {
String str = "programming";
boolean[] seen = new boolean[128]; // covers standard ASCII characters
for (char c : str.toCharArray()) {
if (seen[c]) {
System.out.println("First repeated character: " + c);
break; // found the first repeat, no need to scan further
}
seen[c] = true;
}
}
}
Output
Core Logic
For a known character set like ASCII, a fixed-size boolean array can track 'seen' status without the overhead of a HashSet of boxed Characters.
- 1A
boolean[128]array covers every ASCII character, indexed directly by a character's numeric code. - 2
if (seen[c])checks whether this character has appeared before; if so, it's printed immediately and the loopbreaks. - 3Otherwise,
seen[c] = truemarks this character as seen before moving to the next one.
"programming", the second 'r' at index 4 finds seen['r'] already true, so 'r' is reported, same as the HashSet version.Key Point: Indexing a primitive boolean array avoids the per-entry object overhead a HashSet of boxed Characters would add — same O(n) asymptotic space, but a smaller constant factor.
Why: The boolean array has a fixed size of 128 regardless of the string's length, unlike the HashSet, which can grow with the number of distinct characters seen.