Last active
June 15, 2018 17:56
-
-
Save eday69/03b01e4d598d67ebb0ac0fa4a41c33db to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Sorted Union
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
// 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 uniteUnique(arr) { | |
let base=arguments[0]; | |
let rest = []; | |
for (let i=1; i<arguments.length; i++) { | |
rest.push(arguments[i]); | |
} | |
rest.map(nextList => { | |
nextList.map(item => { | |
if (base.indexOf(item) < 0) { | |
base.push(item); | |
} | |
}); | |
}); | |
return base; | |
} | |
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); // [1, 3, 2, 5, 4] | |
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]); // [1, 3, 2, [5], [4]] | |
uniteUnique([1, 2, 3], [5, 2, 1]); // [1, 2, 3, 5] | |
uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]); // [1, 2, 3, 5, 4, 6, 7, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment