Skip to content

Instantly share code, notes, and snippets.

@weberliu
Created March 10, 2018 04:06
Show Gist options
  • Select an option

  • Save weberliu/2ca5e308dd996077cc3dedb5af9650da to your computer and use it in GitHub Desktop.

Select an option

Save weberliu/2ca5e308dd996077cc3dedb5af9650da to your computer and use it in GitHub Desktop.
Find the difference of two arrays.
function arrDiff (a1, a2) {
var a = []
var diff = []
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]]
} else {
a[a2[i]] = true
}
}
for (var k in a) {
diff.push(k)
}
return diff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment