Hello, I’ve just solved the symmetric difference challenge (hope to contribute to the collection of all possible solutions) and would love to have some feedback on how I can make the code more readable, or if there is a more elegant way for the solution. Thank you!
function sym(...args) {
let arr1 = [] // This will hold our accumulated result
for (let i = 0; i < args.length-1; i++) {
if (i == 0) {
let unique1 = [...new Set(args[i])]
let unique2 = [...new Set(args[i+1])]
// Compare args[0] and args[1]
for (let j = unique1.length-1; j >= 0; j--) {
for (let k = unique2.length-1; k >= 0; k--) {
if (unique1[j] == unique2[k]) {
unique1.splice(j, 1)
unique2.splice(k, 1)
}
}
}
let arr = new Set(unique1.concat(unique2))
arr1 = [...arr] // update outer variable
} else {
let unique2 = [...new Set(args[i+1])]
for (let j = arr1.length-1; j >= 0; j--) {
for (let k = unique2.length-1; k >= 0; k--) {
if (arr1[j] == unique2[k]) {
arr1.splice(j, 1)
unique2.splice(k, 1)
}
}
}
let arr = new Set(arr1.concat(unique2))
arr1 = [...arr]
}
}
console.log(arr1)
return arr1;
}
sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);