Skip to content

Instantly share code, notes, and snippets.

@ddeveloperr
Last active October 18, 2015 20:07
Show Gist options
  • Select an option

  • Save ddeveloperr/ca56d7e07994dea44570 to your computer and use it in GitHub Desktop.

Select an option

Save ddeveloperr/ca56d7e07994dea44570 to your computer and use it in GitHub Desktop.
Bonfire: Diff Two Arrays - I had stuck here and finally figured out, pick up the right solution!
function diff(a1, a2) {
var a = {}, diff = [];
for(var i = 0; i < a1.length; i++) {
a[a1[ i ]] = a1[ i ];
}
for(var j=0; j < a2.length; j++) {
if(a[a2[ j ]]) {
delete a[a2[ j ]];
} else {
a[a2[ j ]] = a2[ j ];
}
}
return Object.keys(a).map(function(prop) {
return a[ prop ];
});
}
diff([1, 2, 3, 3], [1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment