Two Non Repeating Elements in Array Using XOR in DSA Python - Time & Space Complexity
We want to understand how the time taken grows when finding two unique numbers in an array where every other number repeats.
How does the number of steps change as the array gets bigger?
Analyze the time complexity of the following code snippet.
def two_non_repeating(arr):
xor_all = 0
for num in arr:
xor_all ^= num
set_bit = xor_all & (-xor_all)
x = 0
y = 0
for num in arr:
if num & set_bit:
x ^= num
else:
y ^= num
return x, y
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 over the array to perform XOR operations.
- How many times: Each loop runs once over all elements, so twice in total.
Each element is processed twice, so the total steps grow directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 XOR operations |
| 100 | About 200 XOR operations |
| 1000 | About 2000 XOR operations |
Pattern observation: Doubling the input roughly doubles the number of operations.
Time Complexity: O(n)
This means the time taken grows linearly with the size of the input 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 non-repeating elements instead of two? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR properties
XOR of a number with itself is 0, and XOR with 0 is the number itself. This helps cancel out repeating elements.Step 2: Apply XOR on all elements
When XOR is applied on the entire array, all repeating elements cancel out, leaving XOR of the two unique elements.Final Answer:
To cancel out all repeating elements and isolate the XOR of the two unique elements -> Option CQuick Check:
XOR cancels duplicates = isolate unique elements [OK]
- Thinking XOR sorts the array
- Using XOR to count frequencies
- Assuming XOR reverses elements
arr to zero before processing?Solution
Step 1: Initialize XOR variable
We start XOR with 0 because XOR with 0 returns the other number unchanged.Step 2: Confirm initialization correctness
Starting with 0 ensures the first XOR operation correctly sets the value to the first element.Final Answer:
xor = 0 -> Option BQuick Check:
Initialize XOR with 0 for correct accumulation [OK]
- Initializing XOR with 1 causes wrong results
- Using first element without XOR 0 initialization
- Setting XOR to None causes errors
arr = [2, 3, 7, 9, 2, 3]
xor = 0
for num in arr:
xor ^= num
print(xor)
Solution
Step 1: XOR all elements in the array
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14.Step 2: Final XOR value
The final XOR after processing all elements is 14.Final Answer:
14 -> Option DQuick Check:
Stepwise XOR = 14 [OK]
- Forgetting XOR is cumulative and order matters
- Mistaking XOR with addition
- Stopping XOR early
arr = [4, 1, 2, 1, 2, 5]
xor = 0
for num in arr:
xor ^= num
set_bit = xor & ~(xor - 1)
num1 = 0
num2 = 0
for num in arr:
if (num & set_bit) > 0:
num1 ^= num
else:
num2 ^= num
print(num1, num2)
Solution
Step 1: Check set_bit calculation
set_bit = xor & ~(xor - 1) correctly isolates rightmost set bit.Step 2: Verify partition and XOR groups
Elements are split by set_bit and XORed separately to find unique elements.Step 3: Confirm output correctness
Code correctly prints the two unique numbers without errors.Final Answer:
No error, code works correctly -> Option AQuick Check:
Correct set_bit and partitioning = code works [OK]
- Misusing bitwise NOT causing wrong set_bit
- Initializing XOR incorrectly
- Using wrong condition in if statement
arr = [10, 20, 10, 30, 40, 20], which of the following Python code snippets correctly finds the two non-repeating elements using XOR and prints them in ascending order?Solution
Step 1: Calculate XOR of all elements
xor accumulates XOR of all elements, isolating XOR of two unique numbers.Step 2: Find rightmost set bit correctly
set_bit = xor & (-xor) correctly isolates the rightmost set bit for partitioning.Step 3: Partition elements and XOR separately
Elements are split by set_bit and XORed to find the two unique numbers.Step 4: Sort and print unique elements
Sorting ensures output is in ascending order as required.Final Answer:
Code in option A correctly finds and prints [30, 40] -> Option AQuick Check:
Rightmost set bit with xor & (-xor) + partition = correct [OK]
- Using incorrect set_bit calculation like xor | (xor - 1)
- Wrong initialization of xor variable
- Incorrect condition in partition if statement
Ready to prove it?
Pass the test and this pattern moves unknown
