Find the First Repeating Character in a String
Return the first character that has already been seen while scanning a string left to right.
Scan the string left to right and keep a set of characters you have already seen. The first character that is already in the set is the first repeating character, because its second occurrence appears earliest. This needs only one pass and O(n) time.
Problem Statement
Given a string, find the first repeating character — the character whose second
occurrence appears earliest as you scan from left to right. For programming
the answer is r, because its repeat is reached before any other character
repeats.
If no character repeats, report that clearly (for example, return none).
Input: A single string s.
Output: The first repeating character, or none if all characters are distinct.
Examples
Input: "programming"
Output: rScanning p, r, o, g, then r again — r is the first character seen twice.
Input: "abcda"
Output: aa, b, c, d are all new; the second a is the first repeat encountered.
Constraints
0 <= s.length <= 10^5Return the character whose repeat is encountered first
Think Before You Code
Reveal the questions to ask yourself first
- As you scan, how do you know a character has appeared before?
- What structure gives fast membership checks?
- When exactly do you stop and report the answer?
- What do you return if you reach the end with no repeats?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Scan once, remembering what you have seen.
- Create an empty
HashSet<Character>calledseen. - Walk the string from left to right.
- For each character
c:- If
seenalready containsc, returnc— its repeat came first. - Otherwise add
ctoseenand continue.
- If
- If the loop finishes with no repeat found, return
none.
The first time contains succeeds, you are at the earliest second occurrence,
which is exactly the first repeating character.
Dry Run
Walk through the example step by step
Scanning "programming":
char | seen before? | action
-----+-------------------+---------------------
p | {} | add p
r | {p} | add r
o | {p,r} | add o
g | {p,r,o} | add g
r | {p,r,o,g} | r already in set -> return "r"
Solution
Reveal the full Java solution
import java.util.HashSet;
import java.util.Set;
public class FirstRepeating {
public static String firstRepeating(String s) {
Set<Character> seen = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (seen.contains(c)) {
return String.valueOf(c);
}
seen.add(c);
}
return "none";
}
public static void main(String[] args) {
System.out.println(firstRepeating("programming")); // r
System.out.println(firstRepeating("abcda")); // a
}
}
The set holds every character seen so far. As soon as the scan meets a character already in the set, that character's second occurrence is the earliest repeat in the string, so it is returned right away. This "first repeat by second occurrence" reading is the common one; if instead you wanted the leftmost character that ever repeats, you would count all characters first and then re-scan.
O(n) where n is the string lengthSpace: O(k) where k is the number of distinct charactersCommon Mistakes
- Adding the character to the set before checking, so nothing ever looks repeated.
- Confusing this with the first non-repeating character problem.
- Continuing to scan after the first repeat instead of returning immediately.
Edge Cases to Test
- An empty string has no repeating character (return none).
- A string of all distinct characters, like "abcd", returns none.
- A string like "aa" returns a on the second character.
Interview Follow-Ups
- How would you instead find the leftmost character that repeats anywhere?
- How would you return the index of the first repeat rather than the character?
- How would you solve it using a boolean[256] array instead of a HashSet?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →