# Symmetric Difference Challenge (DRY)!

Symmetric Difference Challenge (DRY)!
0
#1

This is my solution of Symmetric Difference Challenge!!!
How can i DRY (Don’t Repeat Yourself) the code?!

``````function sym(args) {
var arr = Array.prototype.slice.call(arguments);

function notNull(val) {
return val !== null;
}

for(var x = 0; x < arr.length; x++) {
for (var z = 1; z < arr[x].length; z++) {
if(arr[x][z] == arr[x][z-1]) {
delete arr[x][z-1];
}
}
arr[x] = arr[x].filter(notNull);
}

while(arr.length > 1) {
var temp = [];

temp = arr.splice(0,1).concat(arr.splice(0,1));
temp = temp.reduce(function(a, b) {
return a.concat(b);
});
temp.sort();

for(var i = 1; i < temp.length; i++) {
if(temp[i] == temp[i-1]) {
delete temp[i];
delete temp[i-1];
}
}

temp = temp.filter(notNull);
arr.unshift(temp);
}

arr = arr.reduce(function(a, b) {
return a.concat(b);
});

return arr;
}
``````
0 Likes

#2

When you find a chunk of code that is repeated, you can look at how to turn that into its own function or you can ask yourself if your function should be recursive.

0 Likes