Last active
August 5, 2016 23:11
-
-
Save bvandgrift/3207c06809c105da1fabaad082dd0b1e to your computer and use it in GitHub Desktop.
return an array of unique elements from an arbitrary list of arrays
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
// 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