Почему код не видит значения 3 и 5 из массива. Со всем остальным все нормально.
const arr2 = [1, 2, 2, 3, 6, 5, 6, 2, 3, 7, 8, 9, 5, 4, 6, 4, 3, 2, 2, 6, 5, 5];
arr2.sort((a, b) => a - b);
console.log(countFreq(arr2, 3))
function searchElement2(arr2, el) {
let left = -1;
let right = arr2.length;
while (right - left > 1) {
const mid = Math.floor((left + right) / 2);
if (arr2[mid] === el) {
return mid;
}
if (arr2[mid] > el) {
right = arr2[mid];
} else {
left = mid;
}
}
return -1;
}
function countFreq(arr2, el) {
const posEl = searchElement2(arr2, el);
if (posEl === -1) {
return 0;
}
let i = posEl;
while (arr2[i] === el) {
i--;
}
let j = posEl;
while (arr2[j] === el) {
j++;
}
return j - i - 1;
}