Ad
  • Custom User Avatar

    Because the description of the problem describes the array argument as being potentially very large, iterating over the array manually like in this solution is preferred because it will take advantage of short circuiting out of the iteration.

    If you use [].forEach, [].filter, [].reduce, etc., then you'll be iterating over potentially millions of numbers when you could have potentially returned after index 2.

    Even worse, if you use [].filter or manually populate additional arrays, you'll be doubling the memory used without needing to.

    Even worse, you'll encounter the issue of the [].filter/manually-created arrays needing to be resized as they get larger and larger, populating the garbage collector with even more memory without needing to.

  • Custom User Avatar
  • Custom User Avatar

    I feel like the Math.abs(integers[n] % 2) should be in a seperate function. I posted a similar solution but using the Array builtins.