Ad
  • Custom User Avatar

    Looks good to me. Thanks everyone! :) Marking as resolved.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • 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.