Find the First Non-Repeating Character in a String
Return the first character in a string that appears exactly once.
First count how many times each character appears using an order-preserving map. Then scan the map (or the string) in order and return the first character whose count is exactly one. If every character repeats, report that none exists. Two passes give O(n) time.
Problem Statement
Given a string, find the first character that does not repeat — that is, the
leftmost character that appears exactly once in the whole string. For swiss
the answer is w, because s and i repeat.
If every character repeats and no unique character exists, report that clearly
(for example, return none).
Input: A single string s.
Output: The first non-repeating character, or none if there isn't one.
Examples
Input: "swiss"
Output: ws and i repeat; w is the first character with a count of one.
Input: "aabb"
Output: noneBoth a and b appear twice, so there is no non-repeating character.
Constraints
0 <= s.length <= 10^5Return the first such character in left-to-right order
Think Before You Code
Reveal the questions to ask yourself first
- How do you know how many times each character appears?
- Why is a single pass not enough to know something is unique?
- How do you keep track of which character came first?
- What should you return when nothing qualifies?
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
Use two passes: one to count, one to find.
- Build a
LinkedHashMap<Character, Integer>of character counts in first-seen order. - Iterate the map's entries in order.
- Return the first character whose count is exactly
1. - If no entry has a count of
1, returnnone.
Because the map preserves insertion order, the first entry with count 1 is
also the leftmost unique character in the original string.
Dry Run
Walk through the example step by step
Finding the first non-repeating character in "swiss":
pass 1 (counts, in first-seen order): {s=3, w=1, i=1}
pass 2 (scan entries in order):
entry | count | count == 1?
------+-------+------------
s=3 | 3 | no
w=1 | 1 | yes -> return "w"
Solution
Reveal the full Java solution
import java.util.LinkedHashMap;
import java.util.Map;
public class FirstNonRepeating {
public static String firstNonRepeating(String s) {
Map<Character, Integer> freq = new LinkedHashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
if (entry.getValue() == 1) {
return String.valueOf(entry.getKey());
}
}
return "none";
}
public static void main(String[] args) {
System.out.println(firstNonRepeating("swiss")); // w
System.out.println(firstNonRepeating("aabb")); // none
}
}
The first pass records how often each character occurs. Uniqueness cannot be
decided while still reading the string, which is why the check happens in a
second pass. Because the LinkedHashMap keeps characters in the order they first
appeared, the first entry with a count of 1 is exactly the leftmost
non-repeating character. If the loop finds none, the method returns none.
O(n) where n is the string lengthSpace: O(k) where k is the number of distinct charactersCommon Mistakes
- Trying to decide uniqueness in one pass before the full string is seen.
- Using a plain HashMap, which loses the first-appearance order.
- Forgetting the case where every character repeats and no answer exists.
Edge Cases to Test
- An empty string has no non-repeating character (return none).
- A single character is always non-repeating ("z" returns z).
- A string where all characters repeat, like "aabb", returns none.
Interview Follow-Ups
- How would you return the index of the first non-repeating character instead?
- How would you solve it with a fixed-size int[256] array instead of a map?
- How would you find the first non-repeating character in a stream of characters?
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 →