Hi everyone,
I managed to find a solution to the Sorted Union problem that I’m pretty happy with (see below). However, I would have liked to have tagged filter onto the first expression so that I could return everything in one expression. I couldn’t find a way to do this because of the filter callback, which references arr.indexOf(). Therefore I have to assign arr in one expression then run filter in a second expression.
I was wondering if there is a way to put this all into one expression, that I’m missing? I was thinking that somehow I could set ‘this’ in the filter callback to the array currently being iterated over, but I couldn’t work out how to do that.
function uniteUnique(arr) {
arr = Array
.from(arguments)
.reduce((acc, cur) =>
acc.concat(cur));
return arr.filter((ele, i) => {
return arr.indexOf(ele) === i;
});
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Link to the challenge:
https://www.freecodecamp.org/challenges/sorted-union