Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the XOR of all elements.
DSA Typescript
let xor = 0; for (const num of arr) { xor = xor [1] num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ causes incorrect results.
Using & or | operators does not find unique elements.
✗ Incorrect
We use the XOR operator (^) to combine all elements because XOR of two identical numbers is 0 and XOR with 0 is the number itself.
2fill in blank
mediumComplete the code to find the rightmost set bit in xor.
DSA Typescript
let setBit = xor & -[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 0 instead of xor causes wrong bit isolation.
✗ Incorrect
The expression xor & -xor isolates the rightmost set bit of xor.
3fill in blank
hardFix the error in the loop that separates elements based on setBit.
DSA Typescript
for (const num of arr) { if ((num & [1]) !== 0) { res1 = res1 [2] num; } else { res2 = res2 ^ num; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ causes wrong results.
Using xor instead of setBit in condition causes logic error.
✗ Incorrect
We check if the bit is set using setBit and XOR accumulate the numbers in res1 and res2.
4fill in blank
hardFill both blanks to initialize result variables and return the final unique elements.
DSA Typescript
let res1 = [1], res2 = [2]; return [res1, res2];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with xor or array element causes wrong output.
✗ Incorrect
Initialize res1 and res2 to 0 before XOR accumulation to correctly find unique elements.
5fill in blank
hardFill all three blanks to complete the function that finds two non-repeating elements.
DSA Typescript
function findTwoUnique(arr: number[]): number[] {
let xor = 0;
for (const num of arr) {
xor = xor [1] num;
}
let setBit = xor & -[2];
let res1 = [3], res2 = 0;
for (const num of arr) {
if ((num & setBit) !== 0) {
res1 = res1 ^ num;
} else {
res2 = res2 ^ num;
}
}
return [res1, res2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ causes wrong results.
Initializing res1 with xor causes incorrect output.
✗ Incorrect
Use XOR (^) to combine elements, xor variable to find setBit, and initialize res1 with 0 for accumulation.
