Bird
0
0
DSA Cprogramming~10 mins

Find the Only Non Repeating Element Using XOR in DSA C - 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
Repeat for all elements
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 C
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 element that does not repeat by XORing all elements.
Execution Table
StepOperationCurrent ElementResult Before XORResult After XORVisual State
1Initialize resultN/AN/A0result=0
2XOR with arr[0]202result=2
3XOR with arr[1]321result=1 (2^3=1)
4XOR with arr[2]213result=3 (1^2=3)
5XOR with arr[3]437result=7 (3^4=7)
6XOR with arr[4]374result=4 (7^3=4)
7XOR with arr[5]440result=0 (4^4=0)
8XOR with arr[6]505result=5 (0^5=5)
9End of arrayN/A55Unique element found: 5
💡 All pairs XOR to 0, leaving the unique element 5 in result.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
resultN/A21374055
iN/A0123456End
Key Moments - 3 Insights
Why does XORing pairs of the same number result in zero?
Because XOR of a number with itself is zero, as shown in steps 6 and 7 where result XOR 4 twice cancels out to zero.
Why do we start result with zero?
Zero is the identity for XOR, so XORing zero with any number returns that number, as in step 2.
How does XOR help find the unique element?
All repeating elements cancel out because XORing a number twice is zero, leaving only the unique element, as seen in the final result 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of result after XORing with arr[3]?
A3
B7
C4
D1
💡 Hint
Check Step 5 in the execution_table where arr[3] = 4 is XORed.
At which step does the result become zero during the XOR process?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look at the 'Result After XOR' column in Step 7.
If the array had no unique element, what would the final result be?
AZero
BThe first element
CThe last element
DUndefined
💡 Hint
Pairs XOR to zero, so no unique element means result ends at zero (see exit_note).
Concept Snapshot
Find unique element by XORing all array elements.
XOR pairs cancel out to zero.
Start with result = 0.
Final result is the unique element.
Works only if every other element repeats exactly twice.
Full Transcript
This concept finds the only non-repeating element in an array by using XOR operation. We start with a variable result set to zero. Then we XOR result with each element in the array one by one. Because XOR of a number with itself is zero, all repeating elements cancel out. The only element that does not repeat remains in result at the end. This method is efficient and uses constant extra space.