Loading
Loading
A conference printed badges numbered 1 to n, but the printer jammed: the stack now holds n + 1 badges, every number between 1 and n, and exactly one badge number appears more than once (it may appear many times, in which case some other numbers are missing).
Return the badge number that appears more than once in badges.
badges = [3, 1, 4, 2, 3][3, 1, 4, 2, 3]3 belongs at index 2: swap it there[4, 1, 3, 2, 3]4 belongs at index 3: swap[2, 1, 3, 4, 3]2 belongs at index 1: swap[1, 2, 3, 4, 3]indices 0–3 hold their own numberindex 4 holds 3index 2 already holds 3 — the reprint3Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Five badges numbered 1 to 4: badge 3 was printed twice.
Example 2
Three badges in the range 1 to 2: badge 2 appears three times, and badge 1 is missing entirely.