Skip to content

Instantly share code, notes, and snippets.

@bvandgrift
Last active August 5, 2016 23:11
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 bvandgrift/3207c06809c105da1fabaad082dd0b1e to your computer and use it in GitHub Desktop.
Save bvandgrift/3207c06809c105da1fabaad082dd0b1e to your computer and use it in GitHub Desktop.
return an array of unique elements from an arbitrary list of arrays
// why not let set and spread do all the work?
// arbitrary number of arrays, unique
function unicat(...arrays) {
let collector = [];
arrays.forEach((a) => {collector = collector.concat(a);});
return Array.from(new Set(collector).values());
}
// O(n), i think, if concat() is implemented sanely.
let a = [1,2,3];
let b = [2,3,4];
let c = [4,5,6];
let u = unicat(a,b,c);
console.log(u); //=> 1,2,3,4,5,6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment