# Symmetric Difference\*\*

**URL:** <https://forum.freecodecamp.org/t/symmetric-difference/450187>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 8, 2021, 8:01pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187 "2021-03-08T20:01:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [March 8, 2021, 8:01pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/1 "2021-03-08T20:01:36Z")

</div>

**Tell us what’s happening:**  
How can I filter the elements which are equal to the elements which are equal to one element in my newArr  
**Your code so far**

```js

function sym(...args) {
  let newArr=[];
  let eins=0;
  let zwei=0;
  let drei=0;
  let vier=0;
  let funf=0;
  let sechs=0;
  let sieben=0;
  let acht=0;
  let neun=0;

  for(let i = 0;i<args[1].length;i++){
             args[0].push(args[1][i])
  }

  for(let i = 0;i<args[0].length;i++){
          if(args[0][i]==1){
            eins+=1
          }
          if(args[0][i]==2){
            zwei+=1
          }
          if(args[0][i]==3){
            drei+=1
          }
          if(args[0][i]==4){
            vier+=1
          }
          if(args[0][i]==5){
            funf+=1
          }
          if(args[0][i]==6){
            sechs+=1
          }
          if(args[0][i]==7){
            sieben+=1
          }
          if(args[0][i]==8){
            acht+=1
          }
          if(args[0][i]==9){
            neun+=1
          }
             
          }
if(eins==1){
   newArr.push(1)

}
if(zwei==1){
   newArr.push(2)

}
if(drei==1){
   newArr.push(3)
}
if(vier==1){
   newArr.push(4)
}
if(funf==1){
   newArr.push(5)
}
if(sechs==1){
   newArr.push(6)
}
if(sieben==1){
   newArr.push(7)
}
if(acht==1){
   newArr.push(8)
}
if(neun==1){
   newArr.push(9)
}

  for(let i=2;i<args.length;i++){
       for(let j=0;j<args[i].length;j++){
          if(newArr[j]!=args[i][j]){
            newArr.push(args[i][j])
         }else{
           newArr.filter(e=>e==args[i][j])//I have the problem here
         }
  
           }
      
  }
  
 
 
}

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

```

```
   **Your browser information:**

```

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36`.

**Challenge:** Find the Symmetric Difference

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/find-the-symmetric-difference)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [March 8, 2021, 9:18pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/2 "2021-03-08T21:18:24Z")

</div>

Hello there,

> [@santiagodeleondecar1](#):
>
> `newArr.filter(e=>e==args[i][j])//I have the problem here`

I am not really going to answer your question, because I assume you want to be able to pass the tests, which means you will need to change a lot more of your code than this one line.

You have far too much hard coding in your script. So, I am not sure if you plan on keeping most of it, but I suggest you reset the code, and start again.

* * *

To this particular line:

> [@santiagodeleondecar1](#):
>
> `newArr.filter(e=>e==args[i][j])`

The `filter` method does not alter the array. It returns a new array. So, this line is just returning a new array, and doing nothing with it.

Also, your function does not return anything. So, it will definitely not pass the tests.

I suggest you start with that, and return with updated code, and a new question. Otherwise, I recommend going through the JavaScript section of the curriculum once more.

Hope this helps

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [March 9, 2021, 7:06pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/3 "2021-03-09T19:06:23Z")

</div>

I think that it could be this way. But, how can I eliminate the duplicates in every array in args?

```auto
function sym(...args) {
  let firstGroup=args[0];
  let secondGroup=args[1];
  let pivotGroup=[];
  let lengthOfargs=args.length
  //console.log(lengthOfargs)
  
   for(let i = 0;i<firstGroup.length;i++){
      if(!secondGroup.includes(firstGroup[i]) && !pivotGroup.includes(firstGroup[i])){
        pivotGroup.push(firstGroup[i])
     }
   }
   for(let i = 0;i<secondGroup.length;i++){
      if(!firstGroup.includes(secondGroup[i]) && !pivotGroup.includes(secondGroup[i])){
        pivotGroup.push(secondGroup[i])
      }
   }
    
// return pivotGroup
  
  
  
    
    for(let i=2;i<args.length;i++){
        for(let j=0;j<args[i].length;j++){
              if(pivotGroup.includes(args[i][j])){
                   
                    pivotGroup=pivotGroup.filter(e=>e!=args[i][j]) 

                      }else{
                        pivotGroup=pivotGroup.push(args[i][j])         
                      }     
            }
      

       }
    
     
      console.log(pivotGroup)
      //return pivotGroup
   
}

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

```

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [March 9, 2021, 7:12pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/4 "2021-03-09T19:12:39Z")

</div>

> [@santiagodeleondecar1](#):
>
> But, how can I eliminate the duplicates in every array in args?

I am seeing errors in the console, because of this line:

```auto
pivotGroup=pivotGroup.push(args[i][j])

```

It is not doing what you expect. Here is the documentation on `.push`:

- [Array.prototype.push() - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)

* * *

Also, what do you expect this to do:

```auto
function sym(...args) {
  let firstGroup=args[0];
  let secondGroup=args[1];

```

What about a 3rd group? Or a 4th group?

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [March 9, 2021, 7:45pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/5 "2021-03-09T19:45:30Z")

</div>

How can I do with those groups? Because they make the pivotGroup, the functions works with that.  
What can I do to elimanate the duplicate? I jave just read methods that employs “hard code”

I think I solved the problem with push.

```auto
function sym(...args) {
                  
  let firstGroup=args[0];
  let secondGroup=args[1];
  let pivotGroup=[];
  let lengthOfargs=args.length;
  //console.log(lengthOfargs)
   if(lengthOfargs==2){
     for(let i = 0;i<firstGroup.length;i++){
      if(!secondGroup.includes(firstGroup[i]) && !pivotGroup.includes(firstGroup[i])){
        pivotGroup.push(firstGroup[i])
     }
   }
   for(let i = 0;i<secondGroup.length;i++){
      if(!firstGroup.includes(secondGroup[i]) && !pivotGroup.includes(secondGroup[i])){
        pivotGroup.push(secondGroup[i])
      }
   }
    //console.log(pivotGroup)
 //return pivotGroup

   }else{
      for(let i=2;i<args.length;i++){
        for(let j=0;j<args[i].length;j++){
              if(pivotGroup.includes(args[i][j])){                                  
                  pivotGroup=pivotGroup.filter(e=>e!=args[i][j]) 
                    }else{
                        pivotGroup.push(args[i][j])         
                      }     
            }
      }
      //console.log(pivotGroup)
       //return pivotGroup
   }

  }

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

```

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [March 13, 2021, 6:46pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/6 "2021-03-13T18:46:02Z")

</div>

I finished it, but how can I refactor it?

```auto
function sym(...args) {
   let newArgs=[];
   for(let i=0;i<args.length;i++){
                 newArgs.push(args[i].filter((a, b) => args[i].indexOf(a) === b))
   }
   let firstGroup=newArgs[0];
   let secondGroup=newArgs[1];
   let pivotArr=[];
   let lengthOfArrs=args.length;
   //console.log(newArgs)     
        if(lengthOfArrs==2){
          for(let i=0;i<secondGroup.length;i++){
             if(!firstGroup.includes(secondGroup[i]) && !pivotArr.includes(secondGroup[i]) ){
                             pivotArr.push(secondGroup[i])
             } 
      }
         for(let i=0;i<firstGroup.length;i++){
             if(!secondGroup.includes(firstGroup[i]) && !pivotArr.includes(firstGroup[i])){
                            pivotArr.push(firstGroup[i])
               }
        }
         
         return pivotArr.sort((a,b)=>a-b)                 
      }else{
        
        for(let i=0;i<secondGroup.length;i++){
             if(!firstGroup.includes(secondGroup[i]) && !pivotArr.includes(secondGroup[i]) ){
                             pivotArr.push(secondGroup[i])
             } 
      }
      
         for(let i=0;i<firstGroup.length;i++){
             if(!secondGroup.includes(firstGroup[i]) && !pivotArr.includes(firstGroup[i])){
                            pivotArr.push(firstGroup[i])
               }
        }
            for(let i = 2;i<newArgs.length;i++){
                  for(let j=0;j<newArgs[i].length;j++){
                        if(pivotArr.includes(newArgs[i][j])){
                          pivotArr=pivotArr.filter(e=>e!=newArgs[i][j])
                    } else{
                                pivotArr.push(newArgs[i][j])
                    }
              }
          } 
          
      }
           //console.log(pivotArr.sort((a,b)=>a-b))
            return pivotArr.sort((a,b)=>a-b)
    }

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

```

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [March 13, 2021, 7:14pm UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/7 "2021-03-13T19:14:22Z")

</div>

Well done, on completing this.

> [@santiagodeleondecar1](#):
>
> how can I refactor it?

There are many ways this could be done. To get a better idea of improvements, I suggest you look at the guide post: [freeCodeCamp Challenge Guide: Find the Symmetric Difference - Guide - The freeCodeCamp Forum](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-find-the-symmetric-difference/301611)

Hope this helps

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [September 12, 2021, 7:15am UTC](https://forum.freecodecamp.org/t/symmetric-difference/450187/8 "2021-09-12T07:15:06Z")

</div>

Closed on September 12, 2021, 7:15am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
