Created
March 10, 2018 04:06
-
-
Save weberliu/2ca5e308dd996077cc3dedb5af9650da to your computer and use it in GitHub Desktop.
Find the difference of two arrays.
This file contains hidden or 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
| 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