# ¿Can someone explain me this recursive function?

I skip the recursive approach in several algorithm challenges in the past, because I couldn´t wrap my head around how the stuff was working.

Now I´m taking a look at this challenge and I´m trying to do solve it with a recursive function:

https://learn.freecodecamp.org/coding-interview-prep/algorithms/find-the-symmetric-difference

I took a look at the solution and I don´t understand how is working. My main doubt is how exactly the function symDiff works, if its really never called with the two arguments its supposed to be called (arrayOne and arrayTwo)

function sym() {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}

function symDiff(arrayOne, arrayTwo) {
var result = [];

arrayOne.forEach(function(item) {
if (arrayTwo.indexOf(item) < 0 && result.indexOf(item) < 0) {
result.push(item);
}
});

arrayTwo.forEach(function(item) {
if (arrayOne.indexOf(item) < 0 && result.indexOf(item) < 0) {
result.push(item);
}
});

return result;
}

// Apply reduce method to args array, using the symDiff function
return args.reduce(symDiff);
}

The explanation: * * The symDiff function finds the symmetric difference between two sets. It is used as a callback function for the reduce() method called on args .* Doesn´t do much to me.

It’s not recursive.

The symDiff function takes two arrays and returns the difference between them.

The callback that is passed to reduce needs to takes a function with two arguments. The first of those is an accumulator and the second is the current value. It returns a new accumulator each time based on doing something with the current value and the accumulator.

You collect all of the arguments at the top of the function.

All the arguments are arrays, so you get an array of arrays.

You run reduce on it. Because the symDiff function works in exactly the way the reduce callback needs to be used, it can be used as the callback.

No initial value is given, so the initial value of the accumulator is the first value in the array, and the current value is the second.

reduce runs symDiff on the first two values, then the callback returns the result, which is an array, which becomes the new accumulator. symDiff is run on this new array and the third value, which returns a new array and so on.

If you want the difference of two arrays,. use Map().

Meaning… insert all your array values into a Map and count each occurrence.

Then return all the values of 1.

That works when all of your arrays have unique values.

In the event that your arrays themselves may have duplicates,. then insert each array into a set,. then insert your sets into a Map using the same strategy.

The reason why using a standard object will fail is because all keys of an object literal will be strings. So 3 and ‘3’ will be treated the same.

You need to understand reduce first. it is a higher order function. syntax of reduce

I can also solve the algorithm like this
notice that I use initialValue for accumulator as an empty array.

function sym(argument) {
// first I check each argument array and make sure there is no duplicate then combine or concat all the array
var args =[];
for (var i = 0; i < arguments.length; i++) {
const arrayWithNoDuplicate = arguments[i].reduce((acc, cur)=>{
const found = acc.find(val=>val===cur)
if(!found){
acc.push(cur);
}
return acc
},[]) // initialvalue
args = [...args, ...arrayWithNoDuplicate]
}

// here I check duplicate again but instead of take one out I take all out
return args.reduce((accumulator, currentValue)=> {
const found = accumulator.find(val => val===currentValue);
if(found){
accumulator.splice(accumulator.indexOf(currentValue),1)
}else{
accumulator.push(currentValue);
};
return accumulator;
},[]); //inital value
}

I hope this self-explain in the code. if not i’m sorry.