# Symmetric Difference

**URL:** <https://forum.freecodecamp.org/t/symmetric-difference/133987>\
**Category:** General\
**Tags:** symmetric, difference, algorithms\
**Created:** [July 4, 2017, 2:09pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987 "2017-07-04T14:09:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nighteye](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nighteye/32/65347_2.png) [@Nighteye](https://forum.freecodecamp.org/u/Nighteye)\
**Post date:** [July 4, 2017, 2:09pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/1 "2017-07-04T14:09:18Z")

</div>

Hello guys, i’m trying to finish the Symmetric Difference challenge but i can’t seem to go anywhere.

I’ve tried every approach i could think of but i can’t seem to find the solution, and i’m starting to get actually stressed from this.

My approach is the following:

- Create an array from the arguments, containing every “set”.
- Reduce the array by comparing the accumulator to the current item, if it matches, remove the element from both arrays.
- Concatenate the accumulator to the current item.

No matter what i do, i can’t seem to get it right, what should i do?

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [July 4, 2017, 2:28pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/2 "2017-07-04T14:28:31Z")

</div>

Try to first state and solve the problem using everyday language avoiding programming terms - use paper and pencil if possible

In everyday terms the problem is there are two lists of numbers - we have to find numbers that are unique to each list

The first test case is:

```
list 1: 1, 2, 3
list 2: 5, 2, 1, 4

```

The answer is:

```
3, 4, 5

```

How would you do this if you did not know any programming? Come up with one solution no matter how clunky or inefficient just as long as it is deterministic and is always guaranteed to give the right answer.

Once you post your solution in everyday language we can think about how to express it in code

---

<div class="post-metadata">

**Author:** ![Nighteye](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nighteye/32/65347_2.png) [@Nighteye](https://forum.freecodecamp.org/u/Nighteye)\
**Post date:** [July 4, 2017, 2:38pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/3 "2017-07-04T14:38:24Z")

</div>

Yeah, i just read the solution out of frustration and… it was basically what i did but i did make the mistake of not creating a new Array.

I feel like i have cheated though.

EDIT:  
Also, i had already defined problem and solution with everyday language, but i did not think of using a third array to store the elements.

---

<div class="post-metadata">

**Author:** ![nr-mihaylov](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nr-mihaylov/32/60307_2.png) [@nr-mihaylov](https://forum.freecodecamp.org/u/nr-mihaylov)\
**Post date:** [July 4, 2017, 3:06pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/4 "2017-07-04T15:06:01Z")

</div>

Even if you do the exercise yourself it is a very good idea to look at the hint. The hint usually shows several different solutions. There is a lot you can learn from figuring out how each one works.

---

<div class="post-metadata">

**Author:** ![userGitHub34535](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/usergithub34535/32/58864_2.png) [@userGitHub34535](https://forum.freecodecamp.org/u/userGitHub34535)\
**Post date:** [July 5, 2017, 6:58pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/5 "2017-07-05T18:58:49Z")

</div>

Hi, I’m having a little trouble deleting repeated elements. For example, if I start with this array  
[[3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]], I’d like to delete the repeated 3s and 6s and get the following array back  
[[3, 2, 5], [2, 1, 5, 7], [3, 4, 6], [1, 2, 3]] but instead I get the following array  
[[3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]] which still unfortunately contains a repeated 6. I can’t seem to spot the code responsible for this, or what I should change - here’s my code below:

```auto
function sym() {
  var args = Array.from(arguments);
  for (var i = 0; i < args.length; i++) {
    for (var j = 0; j < args[i].length; j++) {
      while (args[i].indexOf(j) !== args[i].lastIndexOf(j)) {
        args[i].splice(args[i].lastIndexOf(j),1);       
      }
    }//now we should have deleted all the repeats from all the sets
  }
  return args;
}

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);

```

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [July 5, 2017, 7:34pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/6 "2017-07-05T19:34:05Z")

</div>

A few comments

`arguments` is array-like - there is no need to convert it to another array just for indexing

> **[Arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)**
>
> The arguments object is an Array -like object corresponding to the arguments passed to a function.

You probably want `indexOf` a value in an array not of another index like `j`

It’s generally not a good idea to modify the array a loop iterates over - the array `length` is affected - it’s usually wrong and very confusing at a minimum - instead create and modify a different array to store results

this is another problem that benefits from an initial solution in everyday language - write these lists of numbers on paper and ask yourself how repeated values can be identified and removed

---

<div class="post-metadata">

**Author:** ![userGitHub34535](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/usergithub34535/32/58864_2.png) [@userGitHub34535](https://forum.freecodecamp.org/u/userGitHub34535)\
**Post date:** [July 5, 2017, 7:44pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/7 "2017-07-05T19:44:15Z")

</div>

> [@ppc](#):
>
> You probably want indexOf a value in an array not of another index like j

Thank you! That is something I hadn’t noticed! I amended it with the following code:

```auto
while (args[i].indexOf(args[i][j]) !== args[i].lastIndexOf(args[i][j])) {
        args[i].splice(args[i].lastIndexOf(args[i][j]),1);

```

And then, since you mentioned that arguments is array-like, I can also try,

```auto
for (var i = 0; i < arguments.length; i++) {
    for (var j = 0; j < arguments[i].length; j++) {
      while (arguments[i].indexOf(arguments[i][j]) !== arguments[i].lastIndexOf(arguments[i][j])) {
        arguments[i].splice(arguments[i].lastIndexOf(arguments[i][j]),1);       
      }//delete any repeated elements in a set
    }//now we've deleted all the repeats from all the sets
  }
  
  var myArr = arguments.reduce(function(a, c) {

```

but that threw an error saying that “arguments.reduce() is not a function”, so I kept the arguments assigned to a new array, like it originally was, and now it works yay!! Thank you very much.

```auto
function sym() {
  var args = Array.from(arguments);
  for (var i = 0; i < args.length; i++) {
    for (var j = 0; j < args[i].length; j++) {
      while (args[i].indexOf(args[i][j]) !== args[i].lastIndexOf(args[i][j])) {
        args[i].splice(args[i].lastIndexOf(args[i][j]),1);       
      }//delete any repeated elements in a set
    }//now we've deleted all the repeats from all the sets
  }
  
  var myArr = args.reduce(function(a, c) {
    return a.concat(c);
  });
  
  var symDiffArr = [];
  var parityArr = [];
  var k = 0;
    
  while (myArr.length > 0) {
    var lenAtStart = myArr.length;
    symDiffArr[k] = myArr.shift();
    myArr = myArr.filter(function(v) {
      return v !== symDiffArr[k];
    });
    parityArr[k] = lenAtStart - myArr.length;
    k++;
  }
  
  var resultArr = symDiffArr.filter(function(w, idx) {
    return parityArr[idx] % 2 !== 0; //return true i.e. return odd parities
  });
  return resultArr;
}

sym([3, 3, 3, 2, 5], [2, 1, 1, 5, 7], [3, 4, 6], [1, 2, 3]);

```

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [July 5, 2017, 7:55pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/8 "2017-07-05T19:55:10Z")

</div>

Glad you got it working!

yes - array-like unfortunately means it is not an array - it supports indexing and length but not general array methods - maybe array-unlike is a better term - but the idea is there should be no need to use array functions on `arguments`

---

<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:48pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/133987/9 "2021-01-17T20:48:47Z")

</div>

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


