Skip to content

Instantly share code, notes, and snippets.

@rubenribeiro
Forked from anonymous/bonfire-sorted-union#.js
Created December 6, 2015 23:55
Show Gist options
  • Save rubenribeiro/7b02b9b0e24bcb0e88a1 to your computer and use it in GitHub Desktop.
Save rubenribeiro/7b02b9b0e24bcb0e88a1 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/dannycoder 's solution for Bonfire: Sorted Union
// Bonfire: Sorted Union
// Author: @dannycoder
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sorted-union#
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function unite(arr1, arr2, arr3) {
var newArray = [];
for(var i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++)
{
//add first element to array
if( i === 0 && j === 0) {
newArray.push(arguments[i][j]);
continue;
}
//add next element if not found in the @newArray
if(newArray.indexOf(arguments[i][j]) === -1){
newArray.push(arguments[i][j]);
console.log(arguments[i][j]);
}
}
}
return newArray;
}
unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment