-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16mar23clones.ts
46 lines (32 loc) · 930 Bytes
/
16mar23clones.ts
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 s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
1 <= s.length <= 105
s consists of only lowercase English letters.
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function(s) {
//s is a string with repeating characters
//return no. of characters that don't repeat
//how'd you check for repeating chars?
//loop through the string, see if charAt index matches future indices
// for(var i = 0; i < s.length; i++){
// if(s.indexOf(s[i]) === s.lastIndexOf(s[i])) return i;
// }
// return -1;
const arr = s.split('')
for(let el in arr){
if(arr.indexOf(arr[el]) === arr.lastIndexOf(arr[el])) return el
}
return -1
}