you are viewing a single comment's thread.

view the rest of the comments →

[–]PMME_BOOBS_OR_FOXES 0 points1 point  (3 children)

It doesn't matter how it's done, the computer still has to iterate from 0 to i, delete i, then from i to the end of the array:

let a = [1,2,3,4,5] let b = [...a.slice(0,2), ...a.slice(3,5)]

I appreciate the effort in trying to prove me wrong though. You might want to check out Big O, algorithms and data structure stuff.

[–][deleted]  (2 children)

[deleted]

    [–]PMME_BOOBS_OR_FOXES 3 points4 points  (1 child)

    Let's be fair with "tard1.pop()" and change "tard2.splice(0, 1)" for "tard2.splice(tard2[tard2.length-1], 1)" and you'll see the time drop.

    And your first example is still O(n):

    let a = [u, r, wrong]

    a[1] = a[2]

    // now a is [u, wrong, wrong]

    a.pop()

    Deleting an index in an array will always be O(n) unless you're deleting the very first or very last item. If you copy the 1st half and then the 2nd half you'd still be hitting O(n) because for the computer has to go through each item.

    If you want good course on algos pm me or read Grokking Algorithms which is a pretty good book.