Bird
0
0
DSA Cprogramming~10 mins

Two Non Repeating Elements in Array Using XOR in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Two Non Repeating Elements in Array Using XOR
Start with array
Compute XOR of all elements
Find rightmost set bit in XOR result
Divide elements into two groups based on set bit
Compute XOR of each group separately
Result: two unique non-repeating elements
We first XOR all elements to get XOR of two unique numbers. Then find a set bit to separate elements into two groups and XOR each group to find the unique elements.
Execution Sample
DSA C
int arr[] = {2, 3, 7, 9, 2, 3};
int n = 6;
int xor_all = 0;
for (int i = 0; i < n; i++) {
  xor_all ^= arr[i];
}
This code computes XOR of all elements in the array.
Execution Table
StepOperationXOR All So FarRightmost Set BitGroup 1 XORGroup 2 XORVisual State
1XOR arr[0]=20 ^ 2 = 2[2]
2XOR arr[1]=32 ^ 3 = 1[2,3]
3XOR arr[2]=71 ^ 7 = 6[2,3,7]
4XOR arr[3]=96 ^ 9 = 15[2,3,7,9]
5XOR arr[4]=215 ^ 2 = 13[2,3,7,9,2]
6XOR arr[5]=313 ^ 3 = 1414 & (-14) = 2[2,3,7,9,2,3]
7Divide elements by set bit 2142XOR of elements with bit 2 setXOR of elements without bit 2 set
8Group 1 XOR: 2,3,7,2,320 ^ 2 ^ 3 ^ 7 ^ 2 ^ 3 = 7[Group1: 2,3,7,2,3]
9Group 2 XOR: 920 ^ 9 = 9[Group2: 9]
10Final unique elements279Unique elements: 7 and 9
💡 All elements processed, groups XORed, unique elements found as 7 and 9
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
xor_all021615131414141414
rightmost_set_bit22222
group1_xor00000000777
group2_xor00000000099
Key Moments - 3 Insights
Why do we XOR all elements first instead of directly finding unique elements?
XORing all elements gives XOR of the two unique numbers combined, which helps us find a bit where they differ (see step 6 in execution_table). This bit is used to separate elements into two groups.
How does finding the rightmost set bit help in separating the two unique elements?
The rightmost set bit in xor_all shows a bit where the two unique numbers differ. Dividing elements by this bit ensures each unique number falls into different groups (step 7).
Why do duplicates XOR to zero in their groups?
Because XOR of a number with itself is zero, duplicates cancel out in their groups, leaving only the unique element (steps 8 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the value of xor_all?
A14
B13
C15
D6
💡 Hint
Check the 'XOR All So Far' column at step 6 in execution_table.
At which step do we find the rightmost set bit of xor_all?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for the step where 'Rightmost Set Bit' column is first filled in execution_table.
If the array had no duplicates, how would the 'Group 1 XOR' and 'Group 2 XOR' change?
AThey would both be zero
BThey would be equal
CThey would be the two unique elements themselves
DThey would be the same as xor_all
💡 Hint
Refer to the logic in steps 8 and 9 where groups XOR to unique elements.
Concept Snapshot
Two Non Repeating Elements Using XOR:
1. XOR all elements to get xor_all.
2. Find rightmost set bit in xor_all.
3. Divide elements into two groups by this bit.
4. XOR each group to get unique elements.
Duplicates cancel out by XOR property.
Full Transcript
This concept finds two unique non-repeating elements in an array where all other elements repeat twice. We XOR all elements to get xor_all, which is XOR of the two unique numbers. Then we find the rightmost set bit in xor_all to separate elements into two groups. Each group contains one unique number and duplicates. XORing each group cancels duplicates and reveals the unique numbers. The execution table shows step-by-step XOR calculations and group divisions. Variable tracker follows xor_all, rightmost set bit, and group XORs. Key moments clarify why XOR is used and how groups separate unique elements. Visual quiz tests understanding of xor_all value, step of finding set bit, and group XOR results.