Skip to content

Instantly share code, notes, and snippets.

@danielcodes
Created March 19, 2016 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcodes/3e8c5e25d84cdab99664 to your computer and use it in GitHub Desktop.
Save danielcodes/3e8c5e25d84cdab99664 to your computer and use it in GitHub Desktop.
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];
}