Find the Only Non Repeating Element Using XOR in DSA Java - Time & Space Complexity
We want to know how long it takes to find the unique number in a list where every other number repeats.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
public int findUnique(int[] nums) {
int result = 0;
for (int num : nums) {
result ^= num;
}
return result;
}
This code finds the only number that does not repeat by using XOR on all numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: XOR operation inside a single loop over the array.
- How many times: Once for each element in the array (n times).
Each new number adds one XOR operation, so the work grows directly with the list size.
| 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 number grows directly with the number of elements.
[X] Wrong: "Using XOR means the time is constant no matter the list size."
[OK] Correct: Each element must be processed once, so time grows with the list size, not fixed.
Understanding this helps you quickly find unique elements in data, a common task in coding challenges and real projects.
"What if the array was sorted? Would the time complexity change if we used a different approach?"
Practice
Solution
Step 1: Understand XOR property for identical numbers
XOR of a number with itself always results in 0 because bits cancel out.Step 2: Apply XOR on example
For example, 5 XOR 5 = 0.Final Answer:
It returns 0 -> Option AQuick Check:
X XOR X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR
- Assuming XOR returns 1 for identical numbers
result to 0 in Java?Solution
Step 1: Recall Java variable initialization syntax
In Java, variables are declared with type, name, and assignment using single equals sign.Step 2: Check each option
int result = 0; uses correct syntax:int result = 0;. Others have syntax errors.Final Answer:
int result = 0; -> Option CQuick Check:
Correct Java variable initialization [OK]
- Using '==' instead of '='
- Using ':=' which is not Java syntax
- Assigning undefined variable 'zero'
int[] arr = {2, 3, 2, 4, 3};
int result = 0;
for (int num : arr) {
result ^= num;
}
System.out.println(result);Solution
Step 1: Apply XOR operation on array elements
Start with result=0, XOR with each element: 0^2=2, 2^3=1, 1^2=3, 3^4=7, 7^3=4.Step 2: Final XOR result is the non-repeating element
The only element that does not repeat is 4, which is the final XOR result.Final Answer:
4 -> Option AQuick Check:
Only non-repeating element = 4 [OK]
- Not starting XOR with 0
- Miscomputing XOR steps
- Assuming sum instead of XOR
int[] arr = {1, 2, 1, 3, 2};
int result;
for (int num : arr) {
result ^= num;
}
System.out.println(result);Solution
Step 1: Check variable initialization
The variable 'result' is declared but not initialized before XOR operation, causing a compile error.Step 2: Correct initialization needed
Initialize 'result' to 0 before the loop to avoid error.Final Answer:
Variable 'result' is not initialized before XOR operation -> Option DQuick Check:
Uninitialized variable causes error [OK]
- Forgetting to initialize result
- Misusing loop syntax
- Incorrect print statement
int[] arr = {5, 5, 5, 7, 7, 8, 8};
int result = 0;
for (int num : arr) {
result ^= num;
}
System.out.println(result);Solution
Step 1: Understand XOR property for odd/even counts
XOR cancels even number of identical elements to 0; odd count leaves the element.Step 2: Analyze given array and XOR result
5 appears 3 times (odd) -> XOR of three identical numbers is not the number itself; XOR of 5^5=0, 0^5=5, but this pattern does not hold for triple repeats in general.Step 3: Conclusion
XOR method cannot reliably find the unique element when counts are not exactly twice except one.Final Answer:
Cannot find unique element correctly -> Option BQuick Check:
XOR fails for elements repeating thrice [OK]
- Thinking XOR finds unique element with triple repeats
- Miscomputing XOR of three identical numbers as the number itself
- Ignoring that XOR only cancels pairs
Ready to prove it?
Pass the test and this pattern moves unknown
