# Can I get some help with the Symmetric Difference challenge?

Can I get some help with the Symmetric Difference challenge?
0

#1

Hello everyone. This is my code for the Symmetric Difference challenge. I have been working on it for quite some time now and have come up with some slightly cluttered code. I’m not sure whether I have a logic error, or some sort of bug. Can someone please help me with this?

```````function sym(args) {
var arr = [...arguments];
var result = [];
function diffFirstTwo(arr1, arr2) {
for(var i in arr2) {
if(arr1.includes(arr2[i]) != true && result.includes(arr2[i]) != true) {
result.push(arr2[i]);
} else {
continue;
}
}
for(var j in arr1) {
if(arr2.includes(arr1[j]) != true && result.includes(arr1[j]) != true) {
result.push(arr1[j]);
} else {
continue;
}
}
}

function diffTheRest(arr) {
for(var i in arr) {
if(result.includes(arr[i]) != true) {
result.push(arr[i]);
} else {
var value = arr[i];
for(var j in result) {
if(result[j] === value) {
delete result[j];
j++;
}
}
}
}
}

if(arr.length === 2) {
diffFirstTwo(arr[0], arr[1]);
} else {
diffFirstTwo(arr[0], arr[1]);
for(var i = 2; i < arr.length; i++) {
diffTheRest(arr[i]);
}
}

result = result.filter(function(element) {
return element !== undefined && element !== null;
});
result = result.sort(function(a, b){
return a - b;
});
return result;

}

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

#2

Can you tell us how it’s behaving differently than you expect? Have you identified where it all goes sideways?

#3

On giving this a quick look at i found your code works except for one small problem eg above code should return 1,4,5 but only returns 1,4 … remove one of the 5 in the last array and it will return 1,4,5 … so while double numbers in previous arrays are not causing a problem if the last array has a double and one of the numbers should be in the result it fails here … so you can either debug your code and find out whats different about the way the check is done when you reach last array
or maybe easier would be to remove all double entry’s at the begenning of you code so eg sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]); remove doubles eg var cleared =[ [1, 2, 5], [2, 3, 5], [3, 4, 5]];
and if your wondering how to do that look up using sets in javascript
examples down near bottom of page …so you should create a set for each of the arrays you pass into your function then push them into an array so [2,2,3,5] will become [2,3,5] … any problems post here again

#4

Sure. For all of the tests except the first three, my code returns the symmetric difference of all the arrays, but missing one value. Like JohnL3 said, it returns [1, 4] instead of [1,4,5].