Java String Program · S17
Find the First Non-Repeated Character in Java
Count how often each character appears, then find the first one whose count is exactly one. Two linear passes beat checking every character against every other.
Problem Statement
Write a method that returns the first character in a string that appears exactly once. The comparison is case-sensitive.
If every character repeats, or the string is empty, there is no answer; the method returns null and the program prints none.
Input/Output Examples
"racecar"OutputeExplanation: r, a and c each appear twice; e appears once."aabbcc"OutputnoneExplanation: every character appears twice."Stress"OutputSExplanation: S and s are different, so S appears once and comes first.Visual Explanation
"racecar": r, a and c have count 2, so the first character with count 1 is e (purple). The rest is never read.The first pass builds the full count; the second pass walks the string in its original order, so the first character with count 1 is automatically the earliest unique one.
Approaches
Both approaches count first. They differ in what the second pass walks: the string or the map.
Approach 1: HashMap and Two Passes (Recommended)
Idea: count all characters, then scan the string again and return the first character with count 1.
- Count every character with
merge(c, 1, Integer::sum). - Walk the string from the start.
- Return the first character whose count is 1; if none is found, return
null.
Time: O(n) · Space: O(k).
Approach 2: LinkedHashMap Entries
Idea: count in a LinkedHashMap, whose entries keep first-insertion order, then return the first entry with value 1.
- Count characters in a
LinkedHashMap. - Iterate over
entrySet(). - Return the first key whose value is 1.
Time: O(n) · Space: O(k).
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: HashMap and Two Passes (Recommended)
The order comes from the string itself, so an ordinary HashMap is enough.
import java.util.HashMap;
import java.util.Map;
public class FirstUniqueTwoPass {
// Pass 1 counts every character; pass 2 returns the first one with count 1
static Character firstUnique(String text) {
Map<Character, Integer> counts = new HashMap<>();
for (char c : text.toCharArray()) {
counts.merge(c, 1, Integer::sum);
}
for (char c : text.toCharArray()) {
if (counts.get(c) == 1) {
return c;
}
}
return null; // every character repeats
}
public static void main(String[] args) {
String[] inputs = {"racecar", "aabbcc", "Stress"};
for (String s : inputs) {
Character c = firstUnique(s);
System.out.println("\"" + s + "\" -> " + (c == null ? "none" : c));
}
}
}
"racecar" -> e "aabbcc" -> none "Stress" -> S
Time complexity: O(n) · Space complexity: O(k).
Approach 2: LinkedHashMap Entries
The second pass walks k distinct characters instead of n characters, which helps for long strings with few distinct characters.
import java.util.LinkedHashMap;
import java.util.Map;
public class FirstUniqueLinkedMap {
// LinkedHashMap keeps insertion order, so the first entry with count 1 is the answer
static Character firstUnique(String text) {
Map<Character, Integer> counts = new LinkedHashMap<>();
for (char c : text.toCharArray()) {
counts.merge(c, 1, Integer::sum);
}
for (Map.Entry<Character, Integer> e : counts.entrySet()) {
if (e.getValue() == 1) {
return e.getKey();
}
}
return null;
}
public static void main(String[] args) {
String[] inputs = {"racecar", "aabbcc", "Stress"};
for (String s : inputs) {
Character c = firstUnique(s);
System.out.println("\"" + s + "\" -> " + (c == null ? "none" : c));
}
}
}
"racecar" -> e "aabbcc" -> none "Stress" -> S
Time complexity: O(n) · Space complexity: O(k).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| HashMap and Two Passes | O(n) | O(k) |
| LinkedHashMap Entries | O(n) | O(k) |
Here n is the length of the string and k is the number of distinct characters. Both approaches count in one pass and search in a second pass.
Interview Tips and Recommended Approach
- Avoid
indexOf(c) == lastIndexOf(c)in a loop. It looks neat but scans the string twice for every character:O(n²). - Decide what "no answer" returns.
null, anOptional<Character>or a sentinel such as'_'are all fine if stated. - Ask about case. If
Sandsshould match, lowercase while counting. - Know the streaming variant. For a stream of characters, a
LinkedHashMaplets you answer at any moment.
Recommended Approach: HashMap and Two Passes
Why: it is linear, easy to verify and uses the most common Java collection. The order of the answer comes directly from the input string.