Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e5b95f

Browse files
387. First Unique Character in a String
1 parent 6b63fcf commit 2e5b95f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎easy/FirstUniqueCharacterInAString.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var firstUniqChar = function (s) {
6+
var arr = s.split("");
7+
8+
var obj = [];
9+
for ([i, e] of arr.entries()) {
10+
var arrItem = obj.find((item) => item.ele === e);
11+
var arrIndex = obj.indexOf(arrItem);
12+
if (arrIndex === -1) {
13+
obj.push({ ele: e, count: 1, index: i });
14+
} else {
15+
obj[arrIndex]["count"] += 1;
16+
}
17+
}
18+
var result = obj.find((item) => item.count === 1);
19+
return result?.index >= 0 ? result?.index : -1;
20+
};
21+
22+
x = firstUniqChar("aabb");
23+
console.log(x);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /