Ad
  • Default User Avatar

    Yeah, changing an array is almost always a bad idea, but Array.filter() doesn't do that: it returns a new array.

    Using a counter would be a great approach, since it never incurs the overhead of creating a new array. On the other hand, filter is parallelizable, so it might end up still being faster in practice!

    One way to square both approaches would be to use Array.reduce() to have a counter and avoid changing the original array.

    Thanks for the suggestion!