First unique character

A free easy coding practice problem. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Easy
Type
Function
Languages
JavaScript, TypeScript or Python

Problem

Return the index of the first character in s that appears exactly once, or -1 if there is none.

Function signature

// JavaScript
function firstUniqueChar(s)

// TypeScript
function firstUniqueChar(s: any): any

// Python
def first_unique_char(s):

Examples

  1. firstUniqueChar("leetcode") → 0
  2. firstUniqueChar("aabb") → -1
  3. firstUniqueChar("loveleetcode") → 2

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps