const strs = [null, null, "a", "a", null, "a", null, "b", null, "c"];
const getIndex = (arr, str) => {
if (str === null) {
return -1;
}
let start = 0;
let end = strs.length - 1;
while (start < end) {
let mid = Math.floor((start + end) / 2);
let m = mid;
let res = -1;
while (m >= 0) {
if (strs[m] !== null) {
if (strs[m] === str) {
res = m;
m--;
} else if (strs[m] > str) {
end = m - 1;
break;
} else {
if (res !== -1) {
return res;
}
start = mid + 1;
break;
}
} else {
m--;
}
}
if (m === -1) {
if (res !== -1) {
return res;
} else {
return -1;
}
}
}
if (strs[start] === null) {
return -1;
} else {
if (str === strs[start]) {
return start;
} else {
return -1;
}
}
};
console.log(getIndex(strs, "a"));