 # Solution explanation - Sorted Union algorithm [Spoiler]

So, this is the solution that was partially copy/pasted from the web:

function uniteUnique(arr) {
var sorted =[];
var makeArray = Array.from(arguments);

``````    for (i=0; i<makeArray.length; i++) {
for (x=0; x<makeArray[i].length; x++) {
sorted.push(makeArray[i][x]);
}
}
console.log(sorted);
``````

var final = sorted.reduce(function(a,b){
if (a.indexOf(b)<0){
a.push(b);

``````    }
return a;
},[]);
``````

return final;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

I don’t get this, despite of reading about it on MDN etc. I’m bad with functions… These are the problematic things: if reduce() function takes 4 args (previousValue, currentValue, currentIndex, array), which two of these four are a and b? I thought they are previous and current value, but if so, how can we compare two array elements with indexOf and push one array element (b) into another array element (a), and not into array? Also, why do I get the desired result only when I > return a; ?
Finally, what is > [] in the function?
So, you now know that I don’t get this solution at all…  Can’t type a lot right now. Check out this video and see if it answers some questions: https://youtu.be/Wl98eZpkp-c

1 Like

Oh yes, this guy knows how to explain! Thank you!