Skip to content
SDETRole
SDETRole

First non-repeating character

beginner Map & Set Challenge 3 of 18

Write firstUniqueChar(str), which returns the first character in str that occurs exactly once. The starter returns the first character whatever its count, so it passes some cases by luck.

Requirements

  • Return the character as a one-character string.
  • Return null when there is no such character, including for an empty string.
  • Treat upper and lower case as different characters.
  • Aim for O(n) time.

Expected output

firstUniqueChar('swiss') → 'w' firstUniqueChar('aabb') → null firstUniqueChar('') → null

Test cases (6)

  • ○finds the first unique character
  • ○unique character at the end
  • ○returns null when every character repeats
  • ○returns null for an empty string
  • ○is case-sensitive
  • ○handles a single character
solution.jsExecutes in your browser

Ctrl/⌘ + Enter to run tests · your code is saved in this browser

Results

Run the tests to see results here.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →