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

找出字符串中第一个只出现一次的字符

https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        // let tokens = line.split(' ');
        // let a = parseInt(tokens[0]);
        // let b = parseInt(tokens[1]);
        // console.log(a + b);
        let countMap = {};
        for (let i = 0; i < line.length; i++) {
            const item = line[i];
            if (!countMap.hasOwnProperty(item)) {
                countMap[item] = [i];
            } else {
                countMap[item].push(i);
            }
        }
        let min=Infinity
        for(let i in countMap){
            if(countMap[i].length==1){
                // console.log(countMap[i])
                min=Math.min(min,countMap[i][0])
            }
        }
        let res=(min==Infinity)?-1:line[min]
        console.log(res)
    }
})();

全部评论

相关推荐

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