Skip to content
Advertisement

Detecting mismatch where there are multiple occurences of array elements in more than one array

I have the following arrays:

JavaScript

So I want to display a list of recordIds where we don’t have an equal number of records in either array for given recordId.

e.g In above example recordId:123 has 3 x records in primary array but only 2 x in the secondary array, therefore because the number of records don’t match up for this Id I want to be able to report on this recordId.

I’ve had a go and this is what solution I’ve produced so far using vanilla Javascript:

JavaScript

JavaScript

Running this code produces the following:

JavaScript

So is there a better alternative to the above from a performance standpoint? I’m open to other ideas / suggestions.

Advertisement

Answer

You can use only one map. Will be better for big arrays. It require only one loop. And you can detect if there are more records in the primary ( > 0) or in the secondary ( < 0)

Also you can rewrite reduce in for notation but it make sense only for large arrays

https://measurethat.net/Benchmarks/Show/19290/0/so-two-map-compare

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement