Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Two passes: first build a frequency map, then walk the string and return the first character with count 1. O(n) time.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you use a count-then-scan two-pass and preserve order for 'first'.
Where candidates slip
Iterating the map and losing positional order.
How to stand out
Mention LinkedHashMap / storing first-index as one-structure alternatives.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Frequency then scan
1Hint 1Explore hintHide hint
Can you decide uniqueness before seeing the whole string?
2Hint 2Explore hintHide hint
Pass 1: count all characters. Pass 2: find the first with count 1.
3Hint 3Explore hintHide hint
Re-scan the string (ordered), not the map, for 'first'.
A · Core idea
Two passes: first count every character's frequency, then re-scan the string in order and return the first character whose count is exactly 1. O(n) time.
Why two passes are necessary. You cannot know a character is unique until you've seen the entire string, so the count must be complete before you judge any character. Pass one builds that knowledge; pass two applies it.
Why re-scan the string, not the map. 'First' means first by position in the original string. A hash map has no positional order, so iterating the map could return the wrong character. Re-scanning the input preserves order. (A LinkedHashMap or storing first-index would also work.)
Return convention. When no unique character exists, return the agreed sentinel (a space here, or -1 if returning an index).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "abca"count
a:2, b:1, c:1
table'b'C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The 'count first, then re-scan in order' two-pass shape recurs in many string problems.
ComplexityO(n) time, O(1) space — two linear passes (count, then scan) with a fixed-size count table.
E · Edge cases and pitfalls5s = "leetcode" — 'l' is first with count 1.Explore edge casesHide edge cases
Edge cases handled
s = "leetcode"'l' is first with count 1.s = "aabb"No unique char → ' '.s = ""Empty → ' '.
Pitfalls
- Scanning the (unordered) map instead of the string for 'first'.
- Returning an index when a character is expected (or vice versa).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic char firstUnique(String s) { int[] count = new int[256]; for (int i = 0; i < s.length(); i++) count[s.charAt(i)]++; for (int i = 0; i < s.length(); i++) { if (count[s.charAt(i)] == 1) return s.charAt(i);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Scanning the map (unordered) instead of the string for the 'first' requirement.
- Returning an index instead of the character.
Revise
Carry the pattern into the interview
Pattern to remember
Count then re-scan in order
Code pattern to memorize
count all; then for ch in s: if count[ch]==1 return chRules to remember
- 1Pass 1: frequencies.
- 2Pass 2: first count == 1.
- 3Re-scan the string for order.
Use this pattern when
- 'First/earliest with property' over counts
Interview takeaway
When 'first' matters, re-scan the input, not the map.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Count then re-scan in order. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- First Unique Character in a StringComing soonReturn the index instead of the char.
- Character FrequencyThe count table this relies on.