Created
March 19, 2016 03:00
-
-
Save danielcodes/3e8c5e25d84cdab99664 to your computer and use it in GitHub Desktop.
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
function sym(args) { | |
//solution with ES6 | |
//instead of doing manual work, make set operations | |
//combine values | |
var union = (set1, set2) => new Set([...set1, ...set2]); | |
//Common values, set1 values, if set2 has them, keep them | |
var intersection = (set1, set2) => new Set( [...set1].filter(value => set2.has(value)) ); | |
//keep values that are found only in set1 | |
var difference = (set1, set2) => new Set( [...set1].filter(value => !set2.has(value)) ); | |
//first make the arguments into an array | |
//map each array inside into a set, now we have a list of sets | |
//take union of A & B minus their intersection | |
var result = [...arguments] | |
.map(arr => new Set(arr)) | |
.reduce( (A, B) => difference( union(A, B), intersection(A, B) )); | |
return [...result]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References