Understanding Single Number
Every integer appears exactly twice except one that appears once. Return that single value using XOR cancellation.
How it works
XOR is associative and commutative, so duplicate pairs cancel regardless of their positions.
Start at zero and XOR every number into the accumulator. Only the unpaired value remains.
Try an example
Input: nums = [4, 1, 2, 1, 2]
Output: 4
Equal pairs cancel because x XOR x = 0.
Constraints
- 1–63 signed integers between −1,000,000 and 1,000,000.
- Exactly one value occurs once; every other value occurs twice.
- JavaScript XOR uses signed 32-bit integers; these inputs fit that range.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.