Predict the output
What does this print?
seen: set[int] = set()
out = -1
for b in [4, 9, 2, 9, 4]:
if b in seen:
out = b
break
seen.add(b)
print(out)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 9
Right: 9 is the first value that is already in the set when it comes round again.
The scan stops at index 3, the earliest second sighting, so later repeats never matter.
Why not the others?
- B 4. 4 repeats too, but its second sighting comes after 9's.
- C -1. The loop finds a repeat, so
outis overwritten before the loop ends.