algorythms
Bitwise XOR
LC #136Easy

Single Number

Bitwise XOR
AmazonGoogleMicrosoftApple

Problem

Every element appears twice except one. Find that single element using O(1) space.

arraybit-manipulation

Constraints

  • 1 ≤ n ≤ 3 × 10⁴
  • -3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴
  • Exactly one element appears once; all others appear twice
  • O(1) extra space required

Example

Inputnums = [4, 1, 2, 1, 2]
Output4
Why

1^1=0, 2^2=0, 0^4=4. Every pair cancels; only 4 remains

Hints — reveal one at a time