First request that was retried

EasyAbout 15 minProblem 1 of 3 in Hash mapsDraft

The problem

A gateway logs the ID of each request it receives, in order. A client that retries sends the same ID again.

Return the ID whose second appearance comes earliest in the log, or null if no ID appears twice.

  • 0 ≤ ids.length ≤ 200,000
  • IDs are non-empty strings

Examples

  1. firstRetry(["a1", "b7", "c3", "b7", "a1"])returns "b7"b7 repeats at position 3, before a1 repeats at 4.
  2. firstRetry(["x", "y", "z"])returns null
  3. firstRetry(["q", "q"])returns "q"

Hints

Hint 1 of 2

Walk the log once. For each ID, you only need to know whether it has been seen before.

Hint 2 of 2

A Set answers that in one step. The first ID you find already in the set is the answer.

Editorial

Opens once you solve the problem, or after three checked attempts.

Your solution

Tab indents. Press Esc, then Tab, to leave the editor. Ctrl+Enter runs the examples.

Esc
↑ ↓ to move↵ to open