0
0
DSA Pythonprogramming~10 mins

Two Non Repeating Elements in Array Using XOR in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the XOR of all elements in the array.

DSA Python
def find_xor(arr):
    result = 0
    for num in arr:
        result = result [1] num
    return result

arr = [2, 3, 7, 9, 2, 3, 9, 11]
print(find_xor(arr))
Drag options to blanks, or click blank then click option'
A^
B+
C&
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of XOR.
Using bitwise AND or OR instead of XOR.
2fill in blank
medium

Complete the code to find the rightmost set bit in the XOR result.

DSA Python
def rightmost_set_bit(xor_result):
    return xor_result & -[1]

xor_result = 12
print(rightmost_set_bit(xor_result))
Drag options to blanks, or click blank then click option'
Axor_result
B1
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a constant like 1 instead of the variable.
Using bitwise OR instead of AND.
3fill in blank
hard

Fix the error in the code to correctly separate the two unique numbers using the set bit.

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 & [1]:
            num1 ^= num
        else:
            num2 ^= num
    return num1, num2

arr = [4, 1, 2, 1, 2, 5]
print(find_two_unique(arr))
Drag options to blanks, or click blank then click option'
Anum1
Bxor_all
Cset_bit
Dnum2
Attempts:
3 left
💡 Hint
Common Mistakes
Using xor_all instead of set_bit in the if condition.
Using num1 or num2 instead of set_bit.
4fill in blank
hard

Fill both blanks to complete the function that returns the two unique numbers in sorted order.

DSA Python
def find_two_unique_sorted(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 & [1]:
            num1 ^= num
        else:
            num2 ^= num
    return tuple(sorted(([2], num2)))

arr = [10, 20, 10, 30]
print(find_two_unique_sorted(arr))
Drag options to blanks, or click blank then click option'
Aset_bit
Bxor_all
Cnum1
Dnum2
Attempts:
3 left
💡 Hint
Common Mistakes
Returning xor_all instead of num1.
Using xor_all in the if condition instead of set_bit.
5fill in blank
hard

Fill all three blanks to complete the function that finds two unique numbers using XOR and returns them sorted.

DSA Python
def two_non_repeating(arr):
    xor_all = 0
    for num in arr:
        xor_all ^= num
    set_bit = xor_all & -[1]
    num1 = 0
    num2 = 0
    for num in arr:
        if num & [2]:
            num1 ^= num
        else:
            num2 ^= num
    return tuple(sorted((num1, [3])))

arr = [1, 2, 3, 2, 1, 4]
print(two_non_repeating(arr))
Drag options to blanks, or click blank then click option'
Axor_all
Bset_bit
Cnum2
Dnum1
Attempts:
3 left
💡 Hint
Common Mistakes
Using num1 instead of num2 in the return statement.
Using set_bit instead of xor_all in the negative operation.