Baseline Arrays · Easy O(n) · O(1)
A building turnstile logs every entry and exit, so each badge id appears an even number of times except one badge whose second swipe was lost by the controller. The security team needs that unpaired badge. The answer must be produced in a single pass over the log using no extra storage.
Input: An array nums of n integers where every value appears exactly twice except one value that appears once.
Output: The value that appears exactly once.
1 <= n <= 3 * 10^4-3 * 10^4 <= nums[i] <= 3 * 10^4Every element appears twice except for oneInput: {"nums":[2,2,1]}
Output: 1
The pair 2,2 cancels out and the leftover badge is 1.
Input: {"nums":[4,1,2,1,2]}
Output: 4
Both pairs are matched regardless of position, leaving 4 unpaired.