I’ve said the earlier because what you’ve written so far doesn’t seem to reflect your understanding of the problem.
Anyway, here’s one method which might be what you’ve been thinking (considering all the nested for loops)…
I will call symmetric difference SD from now on. And I won’t use forEach(), map(), and etc… since you seem comfortable with for loop.
You can see handling the SD of arbitrary number of input is calling SD on two inputs successively. For example
SD([1, 2, 5], [2, 3, 5], [3, 4, 5]) => SD( SD([1, 2, 5], [2, 3, 5]), [3, 4, 5] )
So, let’s solve SD that handles two inputs first because handling all input in one function will be messier.
Here’s a psuedocode for solving SD in the most primitive way (in my opinion).
result = []
A = [...], B = [...]
for 1..n in A as a
if B does not include a
add a to the result
for 1..n in B as b
if A does not include b
add b to the result
return unique(result) // because we didn't handle potential duplicates
Here’s a code that does that:
function sd(A, B) {
let res = []
for (let i = 0; i < A.length; ++i) {
if (!B.includes(A[i])) {
res.push(A[i])
}
}
for (let i = 0; i < B.length; ++i) {
if (!A.includes(B[i])) {
res.push(B[i])
}
}
return unique(res)
}
function unique(A) {
let res = {}
for (let i = 0; i < A.length; ++i) {
res[ A[i] ] = A[i]
}
return Object.values(res)
}
Now you want to apply SD many times as I’ve described earlier:
SD(arg1, arg2, arg3...) =>
res1 = SD(arg1, arg2)
res2 = SD(res1, arg3)
res3 = SD(res2, arg4)
...
return res
Here’s a code that does that:
function sdAll(args) {
// Assume at least 2 array arguments
let res = sd(arguments[0], arguments[1])
for (let i = 2; i < arguments.length; ++i) {
res = sd(res, arguments[i])
}
return res
}
Notice you can’t access arguments by index by doing “args[0], args[1] …”. You have to use ‘arguments’ or ES2015 …arg syntax, or the awkward slice.call method.
So, that’s one way to compute symmetric difference. Normally, I wouldn’t post the direct answer but I honestly couldn’t figure out how to start explaining based on what you’ve wrote.
Also, there are better ways of doing this; but I find it less intuitive than this one.