add: 第一个只出现一次的字符
This commit is contained in:
18
src/array/first-unique-character-in-a-string.js
Normal file
18
src/array/first-unique-character-in-a-string.js
Normal 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] || ' '
|
||||
}
|
Reference in New Issue
Block a user