Setting 'this' in filter() - related to Sorted Union problem

Setting 'this' in filter() - related to Sorted Union problem
0.0 0


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
    .reduce((acc, 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:


filter inside the reduce callback.

return Array.from(arguments).reduce((acc, curr) => {
  /* return (concat acc + curr then filter the result) */

(Note that you can implement any of the array functions (map, filter, etc) using reduce - you can almost think of all of the other array functions as just specialised implementations of reduce that do one thing rather than anything)

Also, you can also get rid of the Array.from(arguments) by changing the function signature to:

function uniteUnique(...arrs) {

Then arrs is an array of the arguments.

You can also use Set to remove dupes instead of filter. A set has no duplicates, and silently drops them, so if you convert an array to a set then back to an array you get a unique array, for example (I’m using the rest/spread operator again, but Array.from will work the same):

function uniq(arr) {
  return [ Set(arr)];

uniq([1,1,2,2,3,3,3,3,4,4,4,4,5,5]) // returns [1, 2, 3, 4, 5]


That’s great, thanks for your help!