Sequence NODE_150
MediumFind First Non-Repeating Character
JavaScript
Technical Specification
Given a string, return the first character that does not repeat. If none exists, return null.
Input/Output Samples
Input:"aabbcde"
Output:"c"
Input:"aabb"
Output:null
Optimal Logic Path
function firstNonRepeatingChar(str) {
const freq = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
for (const ch of str) {
if (freq[ch] === 1) return ch;
}
return null;
}Architectural Deep-Dive
We store counts in an object, then scan again to find the first character with frequency 1.