Skip to main content

Find the number that appears once, and the other numbers twice

Problem Statement:​

Given a non-empty array of integersΒ arr, every element appears twice except for one. Find that single one.

Example 1:
Input Format: arr[] = {2,2,1}
Result: 1
Explanation: In this array, only the element 1 appear once and so it is the answer.

Example 2:
Input Format: arr[] = {4,1,2,1,2}
Result: 4
Explanation: In this array, only element 4 appear once and the other elements appear twice. So, 4 is the answer.


βœ… Solution: Bit Manipulation (XOR)​

class Solution {
public:
int singleNumber(vector<int>& numbers) {
// XOR of all numbers β€” duplicates cancel out, only the unique one remains
int uniqueNumber = 0;
for (int i = 0; i < numbers.size(); i++) {
uniqueNumber ^= numbers[i]; // XOR each number into the result
}
return uniqueNumber;
}
};


πŸ“ How It Works​

  • XOR has properties:
    • a ^ a = 0
    • a ^ 0 = a
    • XOR is commutative and associative, so order doesn’t matter.
  • In an array where every element appears twice except one, XOR-ing all elements will cancel out the pairs and leave the single unique number.

🧩 Key Formula​

uniqueNumber = nums[0] ^ nums[1] ^ ... ^ nums[n-1]

β†’ All pairs cancel out due to x ^ x = 0


⏱️ Time & Space Complexity​

MetricValue
⏱ TimeO(n)
πŸ—‚ SpaceO(1)

⚠️ Edge Cases​

  • All elements are duplicates except one β†’ works βœ…
  • Only one element in the array β†’ returns that element βœ…
  • Array not sorted β†’ no issue, XOR works regardless of order βœ…

πŸ’‘ Other Approaches​

ApproachTimeSpaceNotes
XOR (this)O(n)O(1)βœ… Most optimal
HashMap (count freq)O(n)O(n)βœ… But extra space needed
Sorting + Compare AdjO(n log n)O(1)❌ Slower due to sorting

πŸ’¬

Discussion & Doubts