Javascript Algorithm String find fist unique char

Description

Javascript Algorithm String find fist unique char


var firstUniqChar = function(s) {
  var strMap = s.split('').reduce(function(memo, el) {
    memo[el] ? memo[el] += 1 : memo[el] = 1

    return memo//from  w  ww. j a  va 2 s. c o  m
  }, {})

  for(var i = 0; i < s.length; i++) {
    if(strMap[s[i]] === 1)
      return i
  }

  return -1
};

console.log(firstUniqChar("loveleetcode"))



PreviousNext

Related