DSAHashingTwo passes

First Unique Character Visualizer

What is the First Unique Character

A unique character appears exactly once. Hashing counts every letter first; a second left-to-right pass returns the first letter whose count is 1. Two linear passes beat scanning the string for every index.

  • Pass 1: build frequencies — you cannot know uniqueness from a single sighting.
  • Pass 2: the original order decides “first”.
  • Sentinel: if nothing has count 1, return -1.

How this visualizer works

String leetcode. After counting, the second pass stops at l (count 1). e is skipped because it appears three times.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def firstUniqChar(s):
2freq = {}
3for ch in s: freq[ch] = freq.get(ch,0)+1
4for i, ch in enumerate(s):
5if freq[ch] == 1:
6return i
7return -1

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def firstUniqChar(s):
2freq = {}
3for ch in s: freq[ch] = freq.get(ch,0)+1
4for i, ch in enumerate(s):
5if freq[ch] == 1:
6return i
7return -1

complexity.md

OperationTime
Count passO(n)
First-unique passO(n)
SpaceO(Σ)

Python implementation

solution.py

1def firstUniqChar(s):
2 freq = {}
3 for ch in s:
4 freq[ch] = freq.get(ch, 0) + 1
5 for i, ch in enumerate(s):
6 if freq[ch] == 1:
7 return i
8 return -1