DEV Community

Discussion on: Breaking Down JavaScript Solutions To Common Algorithmic Questions (Part 1)

 
worsnupd profile image
Daniel Worsnup

Ahh I understand the confusion now. The last example is definitely tricky because it's easy to read it as one loop. In actuality, Math.max has to iterate over the full argument list in order to produce the maximum, making the last example O(N) if you consider N to be the total number of elements across all of the arrays. You could also say the last example is O(N * M) where N is the number of arrays and M is the number of elements in each array, but this assumes that each array has the same length, which may not be the case.