Last active
May 29, 2021 13:17
-
-
Save vaibhavpandeyvpz/ee573b9ce44f37aa3169e2b1aa4183d3 to your computer and use it in GitHub Desktop.
Find the difference in two array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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