[JavaScript] Set Difference of Two Arrays


Find the elements in one array but not in the other, i.e., set difference of two arrays. In mathematical term:

A − B = {x ∈ A and xB}

Try the following demo to get sense of the set difference:

Array A:
Array B:


The idea is to convert the array B to the data structure of key-value pairs, i.e., hash table. The hash table in JavaScript is built-in object type. Then we check if items in array A is in the hash table. If not, append the item to the difference array, and return the difference array after finish.

The following is the code for the demo:

HTML:

Array A: <input type="text" name="arra" value="[1, 2, 3, 4, 5]"><br>
Array B: <input type="text" name="arrb" value="[2, 3, 5, 7, 11]"><br>
<br>
<button type="button" id="getDiff">Get A - B</button>
<input type="text" name="diff"><br>

JavaScript:

var arra = document.querySelector("input[name='arra']");
var arrb = document.querySelector("input[name='arrb']");
var diff = document.querySelector("input[name='diff']");
var btn = document.querySelector("#getDiff");

btn.addEventListener("click", function(e) {
  var arrayA = eval(arra.value);
  var arrayB = eval(arrb.value);
  var arrayDiff = [];

  var hashTable = {};
  for (var i = 0; i < arrayB.length; i++) {
    hashTable[arrayB[i]] = true;
  }

  for (var i = 0; i < arrayA.length; i++) {
    if (!hashTable.hasOwnProperty(arrayA[i])) {
      arrayDiff.push(arrayA[i]);
    }
  }

  diff.value = JSON.stringify(arrayDiff);
});

Tested on: Chromium 64.0.3282.167 on Ubuntu 17.10 (64-bit)


References:

[1][JavaScript] Intersection of Two Arrays
[2][JavaScript] Union of Two Arrays