Find the Only Non Repeating Element Using XOR in DSA C - Time & Space Complexity
We want to know how the time needed to find the unique element changes as the list grows.
How many steps does it take to find the only number that does not repeat?
Analyze the time complexity of the following code snippet.
int findUnique(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result ^= arr[i];
}
return result;
}
This code finds the only non-repeating element by XORing all elements in the array.
- Primary operation: XOR operation inside a single loop.
- How many times: Exactly once for each element in the array (n times).
Each new element adds one XOR operation, so the total steps grow directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 XOR operations |
| 100 | 100 XOR operations |
| 1000 | 1000 XOR operations |
Pattern observation: The number of operations grows in a straight line as input size increases.
Time Complexity: O(n)
This means the time to find the unique element grows directly with the number of elements in the array.
[X] Wrong: "Since XOR is a simple operation, the time is constant no matter the input size."
[OK] Correct: Even though XOR itself is fast, it must be done for every element, so the total time depends on how many elements there are.
Understanding this simple but powerful approach shows you can use bitwise operations to solve problems efficiently, a skill valued in many coding challenges.
"What if the array was sorted? Would the time complexity to find the unique element change?"
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
