-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstUniqueLetter.js
46 lines (36 loc) · 961 Bytes
/
firstUniqueLetter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Given a string, find the first non - repeating character in it and return it's index. If it doesn't exist, return -1.
// Examples:
// s = "leetcode"
// return 0.
// s = "loveleetcode",
// return 2.
// Note: You may assume the string contain only lowercase letters.
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function (s) {
let letterCount = {};
let firstUnique = { seen: false, value: null };
const sArray = s.split("");
if (s.length == 0) {
return -1;
}
for (let char of sArray) {
if (letterCount[char]) {
letterCount[char] = letterCount[char] + 1;
} else {
letterCount[char] = 1;
}
}
for (let letter in sArray) {
if (letterCount[sArray[letter]] == 1) {
return letter;
}
}
return -1;
};
console.log(firstUniqChar("leetcode")); // 0
console.log(firstUniqChar("loveleetcode")); // 2
console.log(firstUniqChar("")); // -1
console.log(firstUniqChar("aa")); // -1