题目:
思路:
将输入字符串生成字典,再找索引最小的次数最小的值
提交:
while(line = readline()) {
let obj = {}
for (let i = 0; i < line.length; i++) {
if(obj[line[i]]) {
obj[line[i]].push(line[i])
} else {
obj[line[i]] = [line[i]]
}
}
let firstKeyIndex = -1
for (let k in obj) {
if (obj[k].length === 1) {
if (firstKeyIndex < 0) {
firstKeyIndex = line.indexOf(obj[k])
} else {
firstKeyIndex = Math.min(firstKeyIndex, line.indexOf(obj[k]))
}
}
}
console.log(firstKeyIndex < 0 ? firstKeyIndex : line[firstKeyIndex])
}