Java ProgramsStringsFind First Repeated Character in a String

Find First Repeated Character in a String in Java

intermediate·  Strings  ·  String

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.

Input
programming
Output
First repeated character: r

Java Program

Java
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

First repeated character: r

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.

How It Works
  1. 1A HashSet<Character> named seen starts empty.
  2. 2For each character, seen.contains(c) checks whether it has already been added.
  3. 3The first time this check succeeds, that character is the answer — it's printed immediately and the loop breaks.
  4. 4If the check fails, the character is new so far, and seen.add(c) records it before moving on.
Scanning "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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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

HashSetcontains()early exit with break

Approach 2: Manual Boolean Array

Java
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

First repeated character: r

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.

How It Works
  1. 1A boolean[128] array covers every ASCII character, indexed directly by a character's numeric code.
  2. 2if (seen[c]) checks whether this character has appeared before; if so, it's printed immediately and the loop breaks.
  3. 3Otherwise, seen[c] = true marks this character as seen before moving to the next one.
Scanning "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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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.

Key Concepts

boolean arrayASCII indexingearly exit with break

Related Programs