Hook: Interviewers love this one because it checks whether you can keep the original order while counting at the same time — a very common coding pattern.
Question: Find first non-repeated character in a string.
Answer: I would scan the string once to count each character, then scan again in the original order and return the first character whose count is 1. This is simple, fast, and avoids missing the left-to-right order. If no such character exists, I would return null or a special marker, depending on the interviewer's requirement.
Interview-Ready Answer: I’d use a frequency map and preserve insertion order. First I count every character in one pass, then I walk the entries in the same order the characters appeared and return the first one with count 1. That gives me O(n) time and O(k) space, and if the string can contain emoji or other non-BMP characters, I’d switch from char iteration to code points.
“First” means leftmost in the original string, not smallest alphabetically. A character is “non-repeated” if it appears exactly once in the whole input. In Java, the clean interview solution usually counts characters with a LinkedHashMap because it keeps the order in which keys were first seen.
null or whatever sentinel your API requires.A brute-force solution checks each character with indexOf and lastIndexOf, but that can become O(n²) because each character may trigger more scanning. A plain HashMap counts correctly, but it does not guarantee iteration order, so you cannot safely use it to find the first unique character. LinkedHashMap gives you both counting and stable order.
| Approach | Order? | Time | Best use |
|---|---|---|---|
| Nested loops | Yes | O(n²) | Only for tiny inputs |
| HashMap | No | O(n) | Counting only |
| LinkedHashMap | Yes | O(n) | Best general answer |
| Array counts | Yes | O(n) | Fixed small alphabet |
null immediately.null or a sentinel.char in Java is a UTF-16 code unit, so emoji may need codePoints() instead of simple character iteration.Memory-wise, think of it like a ticket line: you stamp each person once, then replay the line from the front and pick the first person who only got one stamp.
Real-World Example: Imagine a checkout service that validates a customer reference code typed into a web form. The service wants to highlight the first character that appears only once so support can spot typos or suspicious patterns quickly.
One bug I have seen in code reviews is using a HashMap and then looping through its keys, assuming they come out in the same order they were inserted. In production, that can make the chosen character look random across JVM runs, which is awful for debugging and user trust.
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
runDemo("swiss"); // w
runDemo("character"); // h
runDemo("aabbcc"); // none
runDemo(""); // edge case: empty string
runDemo(null); // edge case: null input
}
private static void runDemo(String s) {
Character result = firstNonRepeatedCharacter(s);
System.out.println("Input: " + s + " -> " + (result == null ? "none" : result));
}
public static Character firstNonRepeatedCharacter(String s) {
// If the input is missing or empty, there is nothing to search.
if (s == null || s.isEmpty()) {
return null;
}
// LinkedHashMap remembers the order in which characters first appeared.
// That matters because the problem asks for the FIRST unique character, not just any unique one.
Map<Character, Integer> counts = new LinkedHashMap<>();
// First pass: count how many times each character appears.
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
counts.put(ch, counts.getOrDefault(ch, 0) + 1);
}
// Second pass over the map: because order is preserved, the first count of 1 is our answer.
for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
}
// No non-repeated character exists.
return null;
}
}Follow-up & Tricky Questions:
char alone, because it is a UTF-16 code unit. Use s.codePoints() and a map keyed by code point if the input can contain supplementary characters.HashMap iteration order stable in practice? No. It can appear stable in small tests, but the Java API does not promise any order, so you must not depend on it.indexOf plus lastIndexOf acceptable? It works for correctness, but it can be O(n²) in the worst case because you repeat scans for many characters. Interviewers usually prefer the O(n) counting approach.Common Mistakes:
HashMap and assuming the keys come back in input order; the correction is to use LinkedHashMap or scan the string again after counting.indexOf/lastIndexOf solution; the correction is to use a frequency map for O(n) time.null, empty strings, or “no unique character”; the correction is to define the return value up front.Memory Hook: Count it, then line it up. First you stamp every character, then you walk the line from left to right and pick the first person with only one stamp.
Cheat Sheet:
LinkedHashMap + two passes.HashMap counts well, but does not preserve order.null or a sentinel when nothing is unique.char.Practice Tasks:
int[26].s.codePoints().