Editor

First One of a Kind

#42 · Dictionaries and sets · Beginner · 40 XP · about 8 min

Write first_unique(items) that finds the first item in the list that appears exactly once, and returns its index (its position, counting from 0). If every item appears more than once, return -1.

The items can be numbers or strings.

Examples

first_unique([4, 5, 4, 1, 5, 7])    # returns 3
first_unique(['a', 'b', 'a', 'b'])  # returns -1
first_unique(['x'])                 # returns 0

In the first example both 1 and 7 appear once, and 1 comes first, at index 3.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.