Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active May 29, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavpandeyvpz/ee573b9ce44f37aa3169e2b1aa4183d3 to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/ee573b9ce44f37aa3169e2b1aa4183d3 to your computer and use it in GitHub Desktop.
Find the difference in two array.
var array1 = [1, 3, 4];
var array2 = [5, 4, 2];
var existing = [];
for (var i = 0; i < array1.length; i++) {
var key = array1[i];
existing[key] = true;
}
for (var i = 0; i < array2.length; i++) {
var key = array2[i];
if (existing[key]) {
delete existing[key];
} else {
existing[key] = true;
}
}
var differences = [];
for (var k in existing) {
differences.push(k);
}
console.log(differences);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment