# Sorted Union Intermediate explanation

**URL:** <https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786>\
**Category:** General\
**Created:** [August 30, 2017, 12:15pm UTC](https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786 "2017-08-30T12:15:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjoerdkoolen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sjoerdkoolen/32/51271_2.png) [@sjoerdkoolen](https://forum.freecodecamp.org/u/sjoerdkoolen)\
**Post date:** [August 30, 2017, 12:15pm UTC](https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786/1 "2017-08-30T12:15:42Z")

</div>

Could someone try to help me understanding what is going on in the intermediate solution of Sorted Union?

I am having difficulties understanding we have concat going on inside a reduce… Also… stupid question but how are arrA and arrB different? How should I read them?

```auto
function uniteUnique(arr1, arr2, arr3) {
 var newArr;
 //Convert the arguments object into an array
  var args = Array.prototype.slice.call(arguments);
  //Use reduce function to flatten the array
  newArr = args.reduce(function(arrA,arrB){
  //Apply filter to remove the duplicate elements in the array
    return arrA.concat(arrB.filter(function(i){
      return arrA.indexOf(i) === -1;
    }));
  });

   return newArr;                    
}

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

```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 1, 2017, 8:09am UTC](https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786/2 "2017-09-01T08:09:06Z")

</div>

[concat()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat?v=a) takes two or more arrays, merge them and returns a new merged array.

First two parameters or `reduce()` are `accumulator` and `currentArrayItem` (as no default `accumulator` value is provided initial value of `accumulator` is the first element of the initial array).

Reduce works by taking `accumulator` and `currentArrayItem`, doing some stuff **and returning a new `accumulator`** which is then used on the next iteration.

Therefore `reduce()` part should be read:

```auto
newArr = args.reduce(function(accumulator, currentArrayItem) {
  // Take accumulator and merge it...
  return accumulator.concat(
    // ...with currentArrayItem, but before that filter out...
    currentArrayItem.filter(function(i) {
      // ...values that are already in the accumulator
      return accumulator.indexOf(i) === -1;
    })
  );
  // return new merged array and use it as the accumulator
  // on the next iteration or return it if this was the last element
  // of args array
});

```

---

<div class="post-metadata">

**Author:** ![sjoerdkoolen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sjoerdkoolen/32/51271_2.png) [@sjoerdkoolen](https://forum.freecodecamp.org/u/sjoerdkoolen)\
**Post date:** [September 1, 2017, 8:21am UTC](https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786/3 "2017-09-01T08:21:16Z")

</div>

Hi! Will have to read that a couple of times but I was indeed looking for such an explanation! Really appreciate it!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:40pm UTC](https://forum.freecodecamp.org/t/sorted-union-intermediate-explanation/144786/4 "2021-01-17T20:40:47Z")

</div>

Closed on January 17, 2021, 8:40pm UTC


