Imagine I come across this particular challenge:
And here's how I tackle it:
function bcc(arr) {
let res = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == Math.max(...arr)) {
res++;
}
}
return res;
}
console.log(bcc([4, 4, 3, 1]));
Initially, my solution works well for an array like [4, 4, 3, 1]
. However, when HackerRank presents me with a massive array containing 100,000 elements and expecting an output of 7147
, my code fails due to timeout issues. This resulted in me getting only 5 out of 9 problems correct.
Could you offer me a more efficient solution that not only solves this problem quicker but can also be applied to other challenges as well?