Skip to content

Instantly share code, notes, and snippets.

@Kasahs
Last active August 29, 2015 14:24
Show Gist options
  • Save Kasahs/db11429e2d65e1fc9b4f to your computer and use it in GitHub Desktop.
Save Kasahs/db11429e2d65e1fc9b4f to your computer and use it in GitHub Desktop.
Efficient pure Javascript array union
var array1 = [1,2,3,4,5];
var array2 = [1,2,3,4,5,6,7,8,9,10];
var union = [];
set = {};
for(var j = 0; j < array1.length; j++){
set[array1[j]] = true;
}
for(var k = 0; k < array2.length; k++){
set[array2[k]] = true;
}
for (var item in set){
union.push(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment