Skip to content
RED SAGSkills

The Unpaired Badge

At the end of the day every visitor badge id in the log appears exactly twice (check-in and check-out), except one badge that was never returned and appears once.

Return that id using O(1) extra memory.

Example 1

Input: ids = [4, 1, 2, 1, 2]
Output: 4

Example 2

Input: ids = [7]
Output: 7

Constraints

  • 1 <= ids.length <= 10^5 (odd)
  • -10^6 <= ids[i] <= 10^6
  • Exactly one id appears once; all others appear twice.
Hint 1

XOR of a number with itself is 0, and XOR with 0 changes nothing.

Hint 2

XOR is order-independent, so XOR everything together.

Discussion 0

Join free to post in the discussion.

Good practice for: Embedded Systems Engineer, Game Developer, Backend Developer, Blockchain Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 6 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.