DEV Community

Discussion on: Handling Array Duplicates Can Be Tricky

Collapse
 
bgadrian profile image
Adrian B.G.

By visiting all the keys for each element in the source you are making an algorithm of O(nm). Also if the arrays are long enough it will miss the CPU cache for each source item. Its an efficient way basically.

An alternative is to consume more memory and keep the unique elements in a map and going trough the source only once. This way the checks are O(1).

If the elements are objects the memory overhead will be small as you store references.