Any suggestions for improvements.
function sym(args){
let argArr = [...arguments]; //Array of all the arguments. 2D array.
//A While loop that will run until there is only 1 array left i.e. answering array.
while (argArr.length > 1){
let symArr = []; //An empty array to store the symmetric difference between 2 sets.
//Loop through first set (index 0 of arguments array).
//Conditions: each element of first set is not present in second set and also not in the resulting symmetric difference set (to avoid repetition).
for (let i = 0; i < argArr[0].length; i++){
if (!argArr[1].includes(argArr[0][i]) && !symArr.includes(argArr[0][i])){
symArr.push(argArr[0][i]);
}
}
//Loop through second set (index 1 of arguments array).
//Conditions: each element of second set is not present in first set and also not in the resulting symmetric difference set (to avoid repetition).
for (let i = 0; i < argArr[1].length; i++){
if (!argArr[0].includes(argArr[1][i]) && !symArr.includes(argArr[1][i])){
symArr.push(argArr[1][i]);
}
}
//Now that we have the symmetric difference between 2 sets, replace it with the original 2 sets in the arguments array. Arguments array will get updated and while loop will continue until there is only 1 array left in the arguments array.
argArr.splice(0, 2, symArr);
}
//Convert the 2D array into 1D, sort the elements of resulting array in ascending order.
let result = argArr[0].sort((a, b) => a - b);
return result;
}
// Tests
console.log(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]));
console.log(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]));