Bit Manipulation / CoreXOR PatternEasy

XOR Pattern — Find the Unique Element

XOR all elements: duplicates cancel to 0, the unique one remains. O(N) time, O(1) space.

LC
Practice on LeetCode 136: Single Number
Open Problem

What does it solve?

Given an array where every element appears twice except one, find the unique element. XOR exploits two properties: a ⊕ a = 0 and a ⊕ 0 = a. XOR everything together and pairs cancel out.

XOR Properties

a ⊕ a = 0 // self-cancel
a ⊕ 0 = a // identity
a ⊕ b = b ⊕ a // commutative
(a ⊕ b) ⊕ c = a ⊕ (b ⊕ c) // associative
Interactive Visualization
Current
Running XOR
Cancelled Pair
Answer
Array
0=00000000
Step 0 of 0
Index
—
XOR Result
0
Processed
0
Answer
—
Ready. Press Play or Next to start.
✓ Found
Speed
C++ — Single Number
int singleNumber(vector<int>& nums) {
    int result = 0;
    for (int num : nums)
        result ^= num;  // pairs cancel to 0
    return result;
}
Complexity
Time
O(N)

Single pass through the array.

Space
O(1)

Only one variable to track XOR.


📝 Notes

▼