My function needs to accept any number of lists, then output their symmetric difference. sym([1, 2, 3], [5, 2, 1, 4])
should return [3, 5, 4]
and sym([1, 2, 5], [2, 3, 5], [3, 4, 5])
should return [1, 4, 5]
. How can I deal with the unknown number of arguments? I thought that I could use the single variable args
to create a list of lists, but that isn't working.
function isin(num,arra)
{
for (i=0; i<arra.length; i++)
{
if (num == arra[i]){return true;}
}
return false;
}
function sym(args) {
var syms = [];
console.log(args);
for (i=0; i<args.length; i++)
{
var ins = false;
for (j=0; j<args[i].length; j++)
{
for (k=i+1; k < args.length; k++)
{
if(isin(args[i][j], args[k]))
{
ins = true;
}
}
}
if (ins === false)
{
syms.push(args[i][j]);
}
}
return syms;
}
sym([1, 2, 3], [5, 2, 1, 4]);