Skip to content

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

示例

txt
s = "abaccdeff"
// 返回 "b"

s = ""
// 返回 " "

JS

js
/**
 * @param {string} s
 * @return {character}
 */
const firstUniqChar = function (s) {
  const map = {}
  for (c of s) {
    if (map[c]) {
      map[c]++
    }
    else {
      map[c] = 1
    }
  }
  for (c of s) {
    if (map[c] === 1) {
      return c
    }
  }
  return ' '
}

题链