easyStringArrays & Hashing

First Unique Character in a String

easyTime: O(n)Space: O(1)

Recognize the pattern

find first character that appears exactly oncecharacter frequencypreserve order

Brute force idea

A straightforward first read of First Unique Character in a String is this: For each character, count its occurrences in the entire string. Each character re-scans the whole string. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see First Unique Character in a String is this: Two passes: first count all frequencies in a hash map, then scan left-to-right to find the first character with count 1 (26 letters max). The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout First Unique Character in a String is this: The frequency map gives lookup for any character's count. The second left-to-right pass ensures we find the FIRST unique character by order of appearance. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in First Unique Character in a String usually looks like this: Returning the least frequent character instead of the first unique one. Order matters — you must scan left-to-right in the original string. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Arrays & Hashing Pattern