First Non-Repeating Character

Medium ⏱ 12 min 55% acceptance ★★★★☆ 4.4
Write a function first_unique_char(s) that returns the first character in string s that occurs exactly once, using a dict to count occurrences in one pass and a second pass to find the first singleton. Return None if no character is unique.

Examples

Example 1
Input
first_unique_char('swiss')
Output
'w'
Explanation

s appears 3 times, w and i once; w comes first.

Example 2
Input
first_unique_char('aabb')
Output
None
Explanation

Every character repeats.

Constraints

  • Case-sensitive comparison.

Topics

Dictionariesfrequency

Companies

MicrosoftMeta

Hints

Hint 1

Build counts with a dict, then iterate s in order and return the first char whose count is 1.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.