silver-hands / sss

0 stars 0 forks source link

【Q063】面试题50. 第一个只出现一次的字符 #63

Open fly0o0 opened 4 years ago

fly0o0 commented 4 years ago

面试题50. 第一个只出现一次的字符

fly0o0 commented 4 years ago
/**
 * @param {string} s
 * @return {character}
 */
var firstUniqChar = function(s) {
  if (!s) return ' ';
  let arr = s.split('')
  let hashMap = {}

  for (let v of arr) {
    if (hashMap[v] == null) {
      hashMap[v] = true
    } else {
      hashMap[v] = false
    }
  }

  for (let v of arr) {
    if (hashMap[v]) {
      return v
    }
  }

  return ' '
};