-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst-unique.js
More file actions
30 lines (25 loc) · 808 Bytes
/
first-unique.js
File metadata and controls
30 lines (25 loc) · 808 Bytes
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
var firstUniqChar = function(s) {
var len = s.length;
var table = {};
var non = [];
if(s === "") return -1;
// console.log("the word is " + s);
for (var j = 0; j < len; j++) {
if (table[s[j]] === undefined) {
table[s[j]] = true;
non.push({ letter: s[j], index: j });
} else {
// console.log("...");
// console.log({non});
non = non.filter(function (value) {
// console.log({val: value, letter: s[j]});
return value.letter != s[j];
});
// console.log({non});
}
}
return non.length ? non[0].index : -1;
};
console.log(firstUniqChar("leetcode"));
console.log(firstUniqChar("loveleetcode"));
console.log(firstUniqChar("cc"));