add: 第一个只出现一次的字符

This commit is contained in:
2020-05-14 18:26:00 +08:00
parent 9c2a165583
commit d348c63824
4 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,18 @@
/**
* @param {string} s
* @return {character}
*/
export const firstUniqChar = function (s) {
const due = new Set()
const queue = new Set()
for (const n of s) {
if (queue.has(n)) {
queue.delete(n)
due.add(n)
} else if (!due.has(n)) {
queue.add(n)
}
}
return Array.from(queue)[0] || ' '
}