A double loop is created to go through the string turned array picking each letter and then checking the other letters if they match which the current letter using the isUnique boolean value. if isUnique is true we return the index i of the string . if its not found -1 is returned.
- Time complexity: O(N^2)
- Space complexity: O(1)
function firstUniqChar(s: string): number {
for (let i = 0; i < s.length; i++) {
let isUnique = true
for (let j = 0; j < s.length; j++) {
if (i !== j && s[i] === s[j]) {
isUnique = false
break
}
}
if (isUnique) return i
}
return -1
}