0

I have two arrays where I need to compare values and get the duplicates. I wrote most of the code but seem to be stumped on the comparison.

Here is my code:

function compare(arr1, arr2) {
 for (var i = 0; i< arr1.length; i++) {
 for (var j = 0; j < arr2.length; j++) {
 if (arr1[i] == arr2[j]) {
 console.log[i];
 }
 }
 }
}
compare([5, 3, 2, 5, 1, 6], [6, 4, 2, 7, 10]);

I get the for loops to print all of the numbers, but for some reason the if statement comparison doesn't work. Is there something I am not getting about comparing values in arrays?

I am not looking for a straight up answer but guidance if possible.

asked Feb 12, 2015 at 17:33
8
  • @zzzzBov Sorry that was just a copying error. Commented Feb 12, 2015 at 17:36
  • 4
    Typo: console.log[i]; -> console.log(i); Commented Feb 12, 2015 at 17:36
  • 1
    You don't need a nested loop. Loop over the longest array and for each index check to see (arr2.indexOf(arr1[i]) > -1) if it's in the second array. Output the number to a new array if it is. Commented Feb 12, 2015 at 17:37
  • @Andy, although there are efficient libraries that have solved this problem, I believe OP is trying to figure out how to handle this problem for himself. Commented Feb 12, 2015 at 17:38
  • He said "I am not looking for a straight up answer but guidance if possible." which is what I gave. In a comment. Commented Feb 12, 2015 at 17:39

1 Answer 1

2

Your code is quadratic in time since it iterates the second array for each item in the first array. A linear time solution is to convert the first array into a hash table, and then, for each item in the second one, instantly check if it is in the hash.

function intersect(a, b) {
 var hash = {};
 a.forEach(function(x) { hash[x] = 1 });
 
 return b.filter(function(x) { return hash[x] === 1 });
}
c = intersect([5, 3, 2, 5, 1, 6], [6, 4, 2, 7, 10]);
document.write(c)

Do note, however, that this only works if items to compare are primitives, you cannot put objects in a hash, so the code has to be quadratic:

function intersect(a, b) {
 return a.filter(function(x) {
 return b.indexOf(x) >= 0
 });
}
a = {x:'a'};
b = {x:'b'};
c = {x:'c'};
d = {x:'d'};
i = intersect([a,b,c], [a,b,d]);
document.write(JSON.stringify(i));
 

Regarding your bit about improving your current code, I suggest that you make your javascript more idiomatic, in particular,

  • get used to iteration methods instead of for loops
  • check the repertoire of built-in functions and use them wherever possible
  • and, for sanity's sake, never ever use ==
answered Feb 12, 2015 at 17:38

2 Comments

I got your example to work but I am a bit confused as to how it works. Can you direct me to a resource so that I can read up on it? Especially the iterator methods.
@pptaszek1990: for any Javascript research, MDN is always the starting point. BTW, my naming was incorrect, it's "iteration", not "iterator" methods.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.