0
0
DSA Pythonprogramming~20 mins

Two Non Repeating Elements in Array Using XOR in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of XOR-based two unique elements finder
What is the output of the following Python code that finds two non-repeating elements in an array using XOR?
DSA Python
def find_two_unique(arr):
    xor_all = 0
    for num in arr:
        xor_all ^= num
    set_bit = xor_all & (-xor_all)
    num1 = 0
    num2 = 0
    for num in arr:
        if num & set_bit:
            num1 ^= num
        else:
            num2 ^= num
    return num1, num2

result = find_two_unique([2, 3, 7, 9, 2, 3])
print(result)
A(7, 9)
B(9, 7)
C(2, 3)
D(0, 0)
Attempts:
2 left
💡 Hint
Remember XOR of two same numbers is zero and XOR of all elements gives XOR of unique elements.
Predict Output
intermediate
1:30remaining
Output of XOR partitioning step
What is the value of 'set_bit' after executing this code snippet?
DSA Python
arr = [4, 1, 2, 1, 2, 5]
xor_all = 0
for num in arr:
    xor_all ^= num
set_bit = xor_all & (-xor_all)
print(set_bit)
A0
B4
C5
D1
Attempts:
2 left
💡 Hint
The set_bit isolates the rightmost set bit of xor_all.
🔧 Debug
advanced
2:00remaining
Identify the error in XOR two unique elements code
What error will this code produce when run?
DSA Python
def find_two_unique(arr):
    xor_all = 0
    for num in arr:
        xor_all ^= num
    set_bit = xor_all & (-xor_all)
    num1 = 0
    num2 = 0
    for num in arr:
        if num & set_bit:
            num1 ^= num
        else:
            num2 ^= num
    return num1, num2

print(find_two_unique([1, 2, 1, 3]))
ANo error, outputs (2, 3)
BTypeError
CSyntaxError
DIndexError
Attempts:
2 left
💡 Hint
Check the for loop syntax carefully.
Predict Output
advanced
2:00remaining
Output of XOR unique elements with negative numbers
What is the output of this code that finds two unique elements in an array containing negative numbers?
DSA Python
def find_two_unique(arr):
    xor_all = 0
    for num in arr:
        xor_all ^= num
    set_bit = xor_all & (-xor_all)
    num1 = 0
    num2 = 0
    for num in arr:
        if num & set_bit:
            num1 ^= num
        else:
            num2 ^= num
    return num1, num2

result = find_two_unique([-1, -2, -1, -3, -2, -4])
print(result)
A(-4, -3)
B(-3, -4)
C(-1, -2)
D(0, 0)
Attempts:
2 left
💡 Hint
XOR works with negative numbers as well.
🚀 Application
expert
1:30remaining
Number of items in dictionary after XOR grouping
After running the following code, how many keys will the dictionary 'groups' contain?
DSA Python
arr = [10, 12, 10, 15, 12, 20]
xor_all = 0
for num in arr:
    xor_all ^= num
set_bit = xor_all & (-xor_all)
groups = {}
for num in arr:
    key = 'group1' if num & set_bit else 'group2'
    groups[key] = groups.get(key, 0) + 1
print(len(groups))
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
There are two groups based on the set bit.