Two Non Repeating Elements in Array Using XOR in DSA C - Time & Space Complexity
We want to understand how the time needed grows when finding two unique numbers in an array using XOR.
How does the number of steps change as the array gets bigger?
Analyze the time complexity of the following code snippet.
void findTwoNonRepeating(int arr[], int n, int *x, int *y) {
int xor_all = 0;
for (int i = 0; i < n; i++) {
xor_all ^= arr[i];
}
int set_bit = xor_all & (-xor_all);
*x = 0; *y = 0;
for (int i = 0; i < n; i++) {
if (arr[i] & set_bit)
*x ^= arr[i];
else
*y ^= arr[i];
}
}
This code finds two numbers that appear only once in an array where all others appear twice, using XOR operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops that each go through the entire array once.
- How many times: Each loop runs exactly n times, where n is the array size.
As the array size grows, the number of steps grows roughly twice the size of the array because of two full passes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 XOR operations |
| 100 | About 200 XOR operations |
| 1000 | About 2000 XOR operations |
Pattern observation: The operations grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time needed grows directly in proportion to the number of elements in the array.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply.
Understanding this linear time approach shows you can solve problems efficiently by clever use of bit operations, a skill valued in many coding challenges.
"What if the array had three unique numbers instead of two? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR property for duplicates
XOR of two identical numbers is 0, so XORing all elements cancels out duplicates.Step 2: Result after XORing all elements
The result is XOR of the two unique elements because duplicates cancel out.Final Answer:
To cancel out all repeating elements and isolate the XOR of the two unique elements -> Option AQuick Check:
XOR cancels duplicates = isolate unique elements [OK]
- Thinking XOR sorts the array
- Using XOR to count frequencies
- Assuming XOR reverses elements
Solution
Step 1: Understand XOR identity element
XOR with 0 leaves the number unchanged, so start with 0.Step 2: Initialize XOR variable
Setting xor = 0 allows XORing all elements correctly.Final Answer:
int xor = 0; -> Option BQuick Check:
Start XOR with 0 for correct accumulation [OK]
- Initializing XOR with 1 or -1
- Using NULL for integer variable
- Not initializing XOR variable
int arr[] = {2, 3, 7, 9, 2, 3};
int xor = 0;
for(int i = 0; i < 6; i++) {
xor ^= arr[i];
}
printf("%d", xor);Solution
Step 1: XOR all elements in the array
Calculate 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14.Step 2: Result after XORing all elements
The final XOR value is 14, which is XOR of the two unique elements 7 and 9.Final Answer:
14 -> Option AQuick Check:
XOR all elements = 14 [OK]
- Stopping XOR early
- Miscomputing XOR values
- Assuming output is one unique element
int arr[] = {4, 1, 2, 1, 2, 5};
int xor = 0;
for(int i = 0; i < 6; i++) {
xor ^= arr[i];
}
int set_bit = xor & ~(xor - 1);
int x = 0, y = 0;
for(int i = 0; i < 6; i++) {
if(arr[i] & set_bit)
x ^= arr[i];
else
y ^= arr[i];
}
printf("%d %d", x, y);Solution
Step 1: Check set_bit calculation
set_bit = xor & ~(xor - 1) correctly finds rightmost set bit.Step 2: Verify loops and initialization
Loops run from 0 to 5 (6 elements), x and y initialized to 0 correctly.Final Answer:
No error, code works correctly -> Option DQuick Check:
Code logic and syntax are correct [OK]
- Miscomputing set_bit
- Wrong loop boundaries
- Uninitialized variables
Solution
Step 1: XOR all elements to get XOR of two unique elements
This cancels out duplicates and leaves XOR of unique pair.Step 2: Find rightmost set bit in XOR result
This bit differs between the two unique elements.Step 3: Divide elements into two groups based on this bit and XOR each group
This separates the two unique elements and finds them individually.Final Answer:
XOR all elements, find rightmost set bit in XOR result, divide elements into two groups by that bit, XOR each group separately -> Option CQuick Check:
Use XOR and bit partitioning to find unique elements [OK]
- Sorting instead of XOR
- Using hash map (extra space)
- XORing twice without grouping
Ready to prove it?
Pass the test and this pattern moves unknown
