Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Easy · Free

Single number — every element appears twice except one (XOR)

CodingBit ManipulationFresher
Model answer

Approach

  • XOR is commutative; pairs cancel to 0.
  • Accumulate XOR of all elements.

Java Solution

Java
int singleNumber(int[] nums) {
    int x = 0;
    for (int n : nums) x ^= n;
    return x;
}
Complexity
TimeO(n)
SpaceO(1)
Interview tip: Mention overflow is not an issue for XOR on ints in Java.