
Gabriel F. answered 10/10/19
Full Stack Javascript Engineer & Mentor
There are several ways to find duplicates between two arrays. The naive approach is to loop over either array and use the "indexOf" method on the other array to check if that array has the current element in the loop. If it does, just push it to an array that stores the duplicates as such:
There are some caveats to this approach though that you need to be aware of:
- Since "indexOf" performs a loop on array inside the loop (it is basically a nested loop), this code runs in O(n^2) time (quadratic time). This means that for each item in the outer array there is a check on every item in the inner array. This can be very expensive if you are comparing large arrays, say each with 1000 items (you would have 1000 * 1000 passes, or 1,000,000). If you are using this function to solve a little problem once for two small arrays like the ones in your question this shouldn't be an issue but you should avoid nested loops if you can (and in this case you can as you shall see below).
- This function will only work accurately if you have one instance of each item in each array. For instance, if "arr1 = [a,b,d,f,f,f]" and "arr2 = [a,b,c,d,e,f]" you will yield two different results depending on which array you put in the "for loop". If you loop over arr1 you will have these letters as duplicates: a,b,f,f,f but if you loop over arr2 you have a,b,f. Again, depending on the data you are working with this might work, but it WON'T WORK FOR ALL CASES.
I thought it was important to give you that "solution" because it may work for your specific case. Now that that is out of the way we can look into a more robust approach. The next algorithm used to solve the problem is known as frequency counter pattern (I don't think it is an official name, but it is known as such),
In this algorithm you can use an object to store the element and its count (how many there are, say a: 2, b: 3, etc) from one array and and then check if that element is in the other array. If it is, push that element to an array that stores the duplicates and decrease the count of that item in the object--say "a" is a duplicate and you have "a: 2" in the object; after you push "a" to the duplicates array you subtract one from is value in the object ("a: 1"); if the value is 0 you delete that property from the object.
This would be the code:
This code runs in linear time O(n). Although it seems like it does more work, it is much faster than the first algorithm because it doesn't have the nested loop. This code will work for every case scenario where you want ALL duplicates, so in the case above you would have ["a", "f", "f"] regardless of which array you use in the first or second loops.
I hope this helps.


Gabriel F.
Disregard this part of the text: " if the value is 0 you delete that property from the object." I simplified the code.10/10/19
Gabriel F.
not sure why the code editor messed up the alignment10/10/19