Ad
  • Default User Avatar

    I agree with @cbalci and @igandecki. A single loop that iterates the array once is faster than looping the array multiple times.

  • Custom User Avatar

    Comparing 'for loop' to 'indexOf' is not the correct analysis here. The above solution goes throught the remaining list every time it removes an item. @Igandecki's point is fair. A single pass solution can be more efficent than this one.

  • Custom User Avatar

    Very interesting, I have no idea how this could possibly be true.. I will have to investigate. :)

    Thanks!

  • Custom User Avatar

    @Igandecki

    Time complexity is bounded by indexOf and splice which is the fastest way since we dont have to iterate through the array completely. Strangly even doing indexOf multiple times is faster than interating through the array completely. So it's the best way to do it.

    source: http://jsperf.com/js-for-loop-vs-array-indexof/8

  • Custom User Avatar

    How is this voted up for the best practice?

    It's nice to read and very short, let's even say "clever", but, it makes you go through the array multiple times - every time you run indexOf().

    Very inefficient.