Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Strings & text processingMiddleCommonPractical

Return the first non-repeating character in a string, or `None` when every character repeats.

Answer

Count each character first, then scan the original string in its existing order and return the first character whose count is one. Counter makes the counting step explicit, while the second pass is necessary because a frequency table alone does not tell you which unique character appeared first in the original text.

Strong answer includes

  • counts characters first
  • performs a second ordered pass
  • returns None when no unique character exists

Practical example

Count, then preserve original order
Python
editable · browser sandbox
Result
Run the code to see the result.

The generator expression scans characters lazily in original order and next(..., None) provides the no-match result.

Expected result: w, then None.

#strings#counter#algorithms

Sources

collections — Container datatypesPython Software Foundation · deque, defaultdict, Counter, namedtuple and ChainMap semanticsThe Python Standard LibraryPython Software Foundation · Authoritative behavior for built-in types and standard library modules