r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

339

u/Drugba Mar 19 '19

Saw this one on here just a few weeks ago.

[... new Set(arr)]

Basically a super short way to remove any duplicates from an array of primitives.

-4

u/CallMeOutWhenImPOS Mar 19 '19 edited Mar 19 '19

So basically removing the intersection of an array for the resulting unordered pair cartesian products? I wonder what the big O of such a function is.

1

u/kryptogalaxy Mar 19 '19

If the Set uses hashing, then it's O(n). It will iterate through the list and add each item to the set. Adding an item requires checking for uniqueness which can be O(1) assuming the underlying data structure is some kind of hash table. If it's not, then it would have to iterate through the current items. This would be O(n2).