JavaScript Algorithm Question: Characters that appear only once

JavaScript Algorithm Question: Characters that appear only once

Gives you a string containing only lowercase letters.

Please determine whether there is a character that appears only once in the string.

If it exists, output the first character that satisfies the condition.

If not, output no.

input format

A total of one line, containing a string of lowercase letters.

The length of the data guarantee string does not exceed 100000.

output format

Output the first character that satisfies the condition.

Output if None no.

Input sample:

abceabcd

Sample output:

e
let buf = "";

process.stdin.on("readable", function() {
    
    
    let chunk = process.stdin.read();
    if (chunk) buf += chunk.toString();
});

process.stdin.on("end", function() {
    
    
    let str = buf.split('\n')[0];
    let cnt = {
    
    };

    for (let c of str) {
    
    
        if (c in cnt) cnt[c] ++ ;
        else cnt[c] = 1;
    }

    for (let c of str) {
    
    
        if (cnt[c] === 1) {
    
    
            console.log(c);
            return;
        }
    }

    console.log("no");
});

Guess you like

Origin blog.csdn.net/qq_42465670/article/details/130516730