runtime
318 / 500
question 318 Iterators & Generators · medium

Stop at the first repeated id

An event feed must never deliver the same id twice; a monitor reads the feed and reports the first id it sees again. Write first_repeat(ids): return the first id that has already appeared, or None. The feed is long, so the check counts two things: how many ids you take (stop at the first repeat), and how many == comparisons you make (remember ids in a dictionary, not by scanning a list).

Functionfirst_repeat(ids)
idsany iterable of ids (text)
Returnsthe first id that repeats an earlier one, or None
Budgetno ids taken after the first repeat; on 600 distinct ids, at most 2,000 comparisons
Examples
callfirst_repeat(['48000002730', '48000004214', '48000002730', '48000001239'])returns'48000002730'
callfirst_repeat(['a', 'b', 'c'])returnsNone
callfirst_repeat(['x', 'y', 'y', 'x'])returns'y'

Loading your progress…

Stop at the first repeated id — Python practice · Runtime