Here is my solution to the Sorted Union challenge from Intermediate Algorithm Scripting. I can’t help but feel that my code can be refactored to use reduce more effectively. Any advice would be appreciated.
Here is the text from the challenge:
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) {
var args = Array.from(arguments);
var reducedArr = args.reduce(function(a, b) {
return a.concat(b);
});
var results = [];
for (var i = 0; i < reducedArr.length; i++) {
if (results.indexOf(reducedArr[i]) === -1) {
results.push(reducedArr[i]);
}
}
return results;
}