Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 10, 2026 20:18
Show Gist options
  • Select an option

  • Save Ephraimiyanda/5a2e8d1433610ac0b371bc6538136bd1 to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/5a2e8d1433610ac0b371bc6538136bd1 to your computer and use it in GitHub Desktop.
First Unique Character in a String

Question

Approach

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.

Complexity

  • Time complexity: O(N^2)
  • Space complexity: O(1)

Code

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
}
scrnli_9oEw60Vyu1k783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment