题解 | #第一个只出现一次的字符#

第一个只出现一次的字符

https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c

function FirstNotRepeatingChar(str)
{
    // write code here
    const map = {}
    for (let i = 0; i < str.length; i++) {
        if (map[str[i]]) {
            map[str[i]].count++
        } else {
            map[str[i]] = { count: 1, index: i }
        }
    }
    const values = Object.values(map)
    for (let i = 0; i < values.length; i++) {
        if (values[i].count === 1) return values[i].index
    }

    return -1
}
module.exports = {
    FirstNotRepeatingChar : FirstNotRepeatingChar
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务