I have the following code that is supposed to find the intersection between two strings in an array like this ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
It should give the result: 1,4,13
function FindIntersection(strArr) {
const firstArr = strArr[0].split(", ");
const secondArr = strArr[1].split(", ");
let newArr = [];
let i = 0;
let j = 0;
while(i < firstArr.length && j < secondArr.length) {
let a = firstArr[i] | 0;
let b = secondArr[j] | 0;
if(a === b) {
newArr.push(a);
i++;
j++;
} else if(a > b) {
j++;
} else if (b > a) {
i++;
}
}
strArr = newArr.join(",");
return strArr;
}
When I do not use the bitwise operator | 0
the last element in the array is not accessed properly why?
How does the bitwise operator solve this problem?