DEV Community

loading...

Discussion on: Are one-liners always the best solutions?

Collapse
noclat profile image
Nicolas Torres

And if you really want the best possible perfs, the best option is to iterate once.

It could go like that:

let count = 0;
for (let i = 0, max = 0, n = arr.length; i<n; i++) {
  if (arr[i] > max) {
    max = arr[i];
    count = 1;
  } else if (arr[i] === max) {
    count++;
  }
}
return count;
Enter fullscreen mode Exit fullscreen mode