Bit manipulation · Easy
A reconciliation export contains numeric IDs in arbitrary order. Every ID occurs exactly twice except one that occurs once. The export is already validated; your job is to find the unpaired ID without building another collection.
Return the single integer that occurs once using constant extra memory.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "nums": number[] }.
Fields
- nums contains signed 32-bit integers. Exactly one distinct value appears once; every other distinct value appears twice.
Output
Return value
- Return the unpaired value, including its sign.
- Zero is a valid value and a valid answer.
- The caller guarantees the multiplicity rule; do not add a separate input-validation pass.
Constraints
- 1 <= nums.length <= 100001 and the length is odd.
- -2147483648 <= nums[i] <= 2147483647. These explicit bounds also apply to Python.
- Do not sort or mutate nums.
Examples
Example 1
{
"nums": [
4,
-2,
4
]
}Returns
-2The two copies of 4 cancel; -2 is unpaired.
Example 2
{
"nums": [
0
]
}Returns
0A single zero is valid.
Required complexity
O(n) time and O(1) extra space