Last active
October 18, 2015 20:07
-
-
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!
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 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