What if you could find the odd one out in a crowd with just one simple trick?
Why Find the Only Non Repeating Element Using XOR in DSA C?
Imagine you have a box full of socks where every sock has a matching pair except one. You want to find that single sock without checking each pair one by one.
Checking each sock manually to find the one without a pair takes a lot of time and can easily lead to mistakes, especially if the box is large. You might lose track or miss a sock.
Using XOR, you can quickly find the single sock without a pair by combining all socks in a way that pairs cancel out, leaving only the unique one.
int findUnique(int arr[], int size) {
for (int i = 0; i < size; i++) {
int count = 0;
for (int j = 0; j < size; j++) {
if (arr[i] == arr[j]) count++;
}
if (count == 1) return arr[i];
}
return -1;
}int findUnique(int arr[], int size) {
int result = 0;
for (int i = 0; i < size; i++) {
result ^= arr[i];
}
return result;
}This method allows you to find the unique element in a list instantly, no matter how large the list is.
Finding the one defective product in a batch where all others are identical, without checking each product twice.
Manual checking is slow and error-prone.
XOR cancels out pairs and reveals the unique element.
Efficient and simple solution for finding a single non-repeating item.
Practice
Solution
Step 1: Understand XOR properties
XOR of a number with itself is 0, and XOR with 0 is the number itself.Step 2: Apply XOR on all elements
XORing all elements cancels out duplicates, leaving only the unique element.Final Answer:
The only element that does not repeat -> Option DQuick Check:
XOR cancels duplicates = unique element [OK]
- Thinking XOR sums elements
- Assuming XOR finds max or min
- Confusing XOR with addition
a and b in C?Solution
Step 1: Identify XOR operator in C
The caret symbol (^) is the XOR operator in C.Step 2: Confirm correct usage
Usinga ^ bperforms bitwise XOR between integers a and b.Final Answer:
a ^ b -> Option CQuick Check:
^ is XOR operator in C [OK]
- Using & instead of ^
- Using | instead of ^
- Using + instead of ^
int arr[] = {2, 3, 5, 4, 5, 3, 4};
int result = 0;
for(int i = 0; i < 7; i++) {
result ^= arr[i];
}
printf("%d", result);Solution
Step 1: XOR all elements in the array
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^5=4, 4^4=0, 0^5=5, 5^3=6, 6^4=2.Step 2: Final XOR result is 2
After XORing all elements, duplicates cancel out, leaving 2.Final Answer:
2 -> Option AQuick Check:
XOR cancels duplicates = 2 [OK]
- Not XORing all elements
- Miscomputing XOR steps
- Assuming sum or max instead
int arr[] = {1, 2, 1, 3, 2};
int result = arr[0];
for(int i = 1; i <= 5; i++) {
result ^= arr[i];
}
printf("%d", result);Solution
Step 1: Check loop bounds
Array has 5 elements indexed 0 to 4; loop runs i from 1 to 5 inclusive, causing out-of-bounds access at i=5.Step 2: Correct loop condition
Change loop to i < 5 to avoid accessing arr[5] which is invalid.Final Answer:
Loop condition should be < 5, not <= 5 -> Option AQuick Check:
Array index out of bounds fixed by i < 5 [OK]
- Using <= instead of < in loop
- Ignoring array bounds
- Wrong operator used
Solution
Step 1: Understand XOR limitation
XOR works only when duplicates appear exactly twice; it fails if duplicates appear thrice.Step 2: Use bit counting method
Count bits in all numbers, take modulo 3 of each bit count to isolate the unique element's bits.Final Answer:
No; use bit counting and modulo 3 to find the unique element -> Option BQuick Check:
XOR fails if duplicates appear thrice; bit count works [OK]
- Assuming XOR works for any duplicate count
- Trying to XOR thrice duplicates
- Ignoring bit counting approach
Ready to prove it?
Pass the test and this pattern moves unknown
