> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/hash-table/first-unique-character-in-string.md).

# First Unique Character in String

## Problem

Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.

{% hint style="info" %}
For example:

```
s = "leetcode"
return 0.

s = "loveleetcode"
return 2.
```

{% endhint %}

## Solution

```
class Solution:
    def firstUniqChar(self, s: str) -> int:
        freq = {}
        
        if not s:
            return -1
        
        for i in s:
            if i not in freq:
                freq[i] = 0
            freq[i]+=1
        
        for i in range(len(s)):
            if freq[s[i]] == 1:
                return i
        return -1
```
