Complete the code to find the XOR of all elements in the array.
let xor = 0; for (let i = 0; i < arr.length; i++) { xor = xor [1] arr[i]; }
The XOR operator (^) is used to find the XOR of two numbers. Here, we XOR all elements to get the XOR of the two unique numbers.
Complete the code to find the rightmost set bit in xor.
let setBit = xor & -[1];The expression xor & -xor isolates the rightmost set bit of xor.
Fix the error in the loop that separates elements into two groups based on setBit.
for (let i = 0; i < arr.length; i++) { if (arr[i] & [1]) { num1 = num1 ^ arr[i]; } else { num2 = num2 ^ arr[i]; } }
We use setBit to check if the bit is set in each element to separate them into two groups.
Fill both blanks to initialize variables and return the result array.
let num1 = [1]; let num2 = [2]; return [num1, num2];
Initialize num1 and num2 to 0 before XOR operations to accumulate unique numbers.
Fill all three blanks to complete the function that finds two non-repeating elements.
function findTwoUnique(arr) {
let xor = 0;
for (let i = 0; i < arr.length; i++) {
xor = xor [1] arr[i];
}
let setBit = xor & -[2];
let num1 = [3];
let num2 = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] & setBit) {
num1 = num1 ^ arr[i];
} else {
num2 = num2 ^ arr[i];
}
}
return [num1, num2];
}The function uses XOR to find combined XOR of two unique numbers, isolates a set bit, then separates numbers into two groups to find the unique elements.
