First Non Repeating Character

  • n, 1 (alphabet 26 constant)

function firstNonRepeatingCharacter(str) {
  const memo = {}
  for (let i = 0; i < str.length; i++) {
    const c = str[i];
    if (memo[c] === undefined) memo[c] = 0;
    memo[c] += 1;
  }

  for (let i = 0; i < str.length; i++) {
    const c = str[i];
    if (memo[c] === 1) return i;
  }
  
  return -1;
}

// Do not edit the line below.
exports.firstNonRepeatingCharacter = firstNonRepeatingCharacter;

Last updated