Complete the code to initialize the variable that will hold the XOR result.
let result = [1];We start with 0 because XOR with 0 returns the other number unchanged.
Complete the for loop to iterate over the array elements.
for (let i = 0; i [1] arr.length; i++) {
The loop should run while i is less than the array length to avoid out-of-bounds errors.
Fix the error in the XOR operation inside the loop.
result = result [1] arr[i];The XOR operator in JavaScript is ^, which is used to find the unique element.
Fill both blanks to complete the function that returns the unique element.
function findUnique(arr) {
let result = [1];
for (let i = 0; i [2] arr.length; i++) {
result = result ^ arr[i];
}
return result;
}Initialize result with 0 and loop while i is less than arr.length.
Fill all three blanks to create a concise one-liner function that finds the unique element.
const findUnique = arr => arr.reduce(([1], [2]) => [1] ^ [2], 0);
Use 'acc' and 'curr' as parameter names and XOR them inside reduce, starting with 0.
