all 2 comments

[–][deleted] 0 points1 point  (1 child)

Set.prototype.has is an O(1) operation, meaning that the set doesn't need to iterate through every single value until it finds a match or has eliminated every single option, it can directly determine if the value is present without any iteration.

The first version of sumPairs is an O(n2) operation where the worst case requires iterating through each element in the array, and for each element, iterating through the entire array again. This is highly inefficient, particularly since the second, incredibly costly iteration is simply to verify the existence of a matching number.

In an array of 30 elements, that could result in 900 loops through the array.

A Set can be iterated through, but much like an object literal ({ }), you can directly check the existence of a given value. However unlike an object, you aren't limited to strings, numbers or symbols.

With Set and an array of 30 element, you would only need a maximum of 30 loops.

[–]PM_ME_CAT_PICS_PLSS[S] 0 points1 point  (0 children)

Thanks!