All examples

First unique character, .

Two passes with a Map: count everything, then find count === 1.

first-unique-character.js
function firstUnique(s) {
  const counts = new Map();
  for (const ch of s) {
    counts.set(ch, (counts.get(ch) ?? 0) + 1);
  }
  for (let i = 0; i < s.length; i++) {
    if (counts.get(s[i]) === 1) return i;
  }
  return -1;
}

firstUnique("swiss");

What to watch

Watch the Map fill with counts, then the second loop stop at the answer.

Reading first unique characteron a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how first unique character works.

Run first unique character yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow