title: Javascript: efficiently compare two integer arrays
body: <p>I have two integer arrays which contain numeric values. I want to look through both lists and check for commonality (or lack of) between the lists. I.e. I want to iterate through the array(s) and find those items which appear in both lists, while in a separate function I want to go through the arrays and find items which are in the first and not in the second.</p>
  
  <p>The obvious way of doing this is nested for loops:</p>
  
  <pre><code>var containedInFirst = false;
  for (var primaryID = 0; primaryID &lt; PrimaryArray.length; primaryID++) {
          containedInFirst = false;
          for (var secondaryID = 0; secondaryID &lt; SecondaryArray.length; secondaryID++) {
              if (PrimaryArray [primaryID] === SecondaryArray[secondaryID]) {
                  containedInFirst = true;
                  break;
              }
          }
  
  //Do some more stuff based on the value of containedInFirst here
  }
  </code></pre>
  
  <p>But given the lists could contain hundreds or thousands of records this is quite a bit of itteration and processor intensive.
  I was therefore wondering if there is a more efficient way of executing the above code? Not just the actual searching, but something more efficient than an Integer array as the container for the values, or just not using nested for loops to traverse and compare the content.</p>
  
  <p>Any thoughts on more efficient or elegant solutions?</p>