0
0
DSA Pythonprogramming~10 mins

Find the Only Non Repeating Element Using XOR in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Find the Only Non Repeating Element Using XOR
Start with result = 0
For each element in array
Apply XOR: result = result XOR element
After all elements processed
Result holds the non-repeating element
Return result
We start with zero and XOR it with every element in the array. Pairs cancel out, leaving only the unique element.
Execution Sample
DSA Python
arr = [2, 3, 2, 4, 4]
result = 0
for num in arr:
    result ^= num
print(result)
This code finds the only element that does not repeat in the array using XOR.
Execution Table
StepOperationCurrent ElementResult Before XORResult After XORExplanation
1Initialize resultN/AN/A0Start with result = 0
2XOR with element2020 XOR 2 = 2
3XOR with element3212 XOR 3 = 1
4XOR with element2131 XOR 2 = 3
5XOR with element4373 XOR 4 = 7
6XOR with element4737 XOR 4 = 3
7End of arrayN/A33Result is 3, the non-repeating element
💡 All elements processed; pairs canceled out leaving only the unique element 3.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
result0213733
current elementN/A23244N/A
Key Moments - 3 Insights
Why does XORing pairs of the same number cancel out?
Because XOR of a number with itself is 0, as shown in steps 5 and 6 where 4 XOR 4 returns to 3 (previous result).
Why start result with 0?
XOR with 0 returns the number itself, so starting with 0 ensures the first element sets the initial result (step 2).
What if the array has more than one non-repeating element?
This method only works if exactly one element is unique; otherwise, the result will be XOR of all unique elements, not a single one.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after processing the third element?
A2
B3
C1
D7
💡 Hint
Check the 'Result After XOR' column at Step 3 in the execution table.
At which step does the 'result' first become 0 after XOR operation?
AStep 2
BNever
CStep 4
DStep 6
💡 Hint
Look at the 'Result After XOR' column for all steps; see if 0 appears.
If the array was [1, 1, 2, 3, 3], what would be the final 'result' after all XOR operations?
A2
B1
C3
D0
💡 Hint
Pairs cancel out; only 2 is unique. Refer to how pairs cancel in the execution table.
Concept Snapshot
Find unique element using XOR:
- Initialize result = 0
- XOR result with each array element
- Pairs cancel out (x XOR x = 0)
- Result holds the only non-repeating element
- Works only if exactly one unique element exists
Full Transcript
To find the only non-repeating element in an array, start with a result variable set to zero. Then XOR this result with each element of the array one by one. Because XOR of a number with itself is zero, all repeating elements cancel out. After processing all elements, the result contains the unique element. This method is efficient and uses constant extra space. It works only if there is exactly one element that does not repeat.