First Repeated Product Scan

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.5
A barcode scanner emits product codes one by one. Write first_repeat(codes) that returns the first code that appears a second time while scanning left to right, or None if every code is unique. A set makes each lookup O(1).

Examples

Example 1
Input
first_repeat(['A1', 'B2', 'C3', 'B2', 'A1'])
Output
'B2'
Explanation

B2 completes its second appearance before A1 does.

Constraints

  • Return the element, not its index.
  • Return None when all are unique.

Topics

Setsseen-tracking

Companies

AmazonWalmartFlipkart

Hints

Hint 1

Track everything seen so far in a set.

Hint 2

The first membership hit is the answer.

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