题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
用到了Array 与 Object之间的转换技巧 完整代码如下:
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()){
//利用object完成了字符串中字母先后顺序的排序 同时标记了相应的个数
let object = line.split('').reduce((a, b) => ({...a, [b]: 1 + (a[b] = a[b] ? a[b] : 0)}), {});
let output = -1;
//利用Object.entries()方法遍历Object中的元素,在碰到第一个个数为1的字母时break,返回output
for (const [k, v] of Object.entries(object)) {
if (v == 1) {
output = k;
break;
}
}
console.log(output);
}
}()


