Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/050f1012ce661904b22f to your computer and use it in GitHub Desktop.
Save anonymous/050f1012ce661904b22f to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Sorted Union
// Bonfire: Sorted Union
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sorted-union
// Learn to Code at Free Code Camp (www.freecodecamp.com)
/* Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order. */
function unite(arr1, arr2, arr3) {
// Creates an empty array to store our final result.
var finalArray = [];
// Loop through the arguments object to truly made the program work with two or more arrays
// instead of 3.
for (var i = 0; i < arguments.length; i++) {
var arrayArguments = arguments[i];
// Loops through the array at hand
for (var j = 0; j < arrayArguments.length; j++) {
var indexValue = arrayArguments[j];
// Checks if the value is already on the final array.
if (finalArray.indexOf(indexValue) < 0) {
finalArray.push(indexValue);
}
}
}
return finalArray;
}
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