First Unique Character in a String | LeetCode 387

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard


Here is the full implementation.

let firstUniqChar = function(s) {
  let frequencies = {};
  let result = -1;

  for (let char of s) {
    if (frequencies[char] === undefined) {
      frequencies[char] = 1;
    } else {
      frequencies[char]++;
    }
  }

  for (let i = 0; i < s.length; i++) {
    let char = s.charAt(i);
    if (frequencies[char] === 1) {
      return i;
    }
  }

  return result;  
};