The purpose of this blog post is to go over the Symmetric Difference challenge, I’ve recently completed. The objective of this challenge was to…”Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays.

Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term “symmetric difference” of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).”

Here are links that were give to help solve this problem:

To solve this problem, I started off by removing the duplicates in the individual arrays contained in the array passed as an argument to the sym function. I did this in order to convert the arrays of number into mathematically correct sets as described here.

The next step was to compare the first and second sets to find the symmetic set that resulted from the comparison. Then, the symmetric set was compared to the next set (array) to find a new symmetic set. This process is repeated until all the arrays have been evaluated and the final symmetic set is found.

Next, I created a function called “compareArr” that compares two arrays and returns a symmetric set by doing the following:

- Iterate through one of the arrays (“arr1”) to get a number in its set.
- Looked for the number in “arr2”.
- If the number was not found, the number was put into an array called “unique”. If the number was found, the number was removed from both arrays.
- After completion of iterating through arr1, any thing left in arr 2 was put into the unique array.
- The process results in unique containing a symmetric set.

To make the loop calling the compareArr function simpler, I compared the first array to a empty array that would eventually hold the final symmetic set (“symmetricSet”). This allowed me to always compare symmetricSet to individual arrays. Otherwise, I would have had to write conditional code to do the first comparison between the first and second arrays, and then compare subsequent arrays to symmetricSet.

function sym(passedInArguments) { //console.log(arguments); //create a variable that will hand the numbers w/o duplicates var symmetricSet = []; //creat a variable for the arguments passed into the sym function var args = arguments; for (var i = 0; i < args.length; i++) { //filter through each individual array and remove the duplicates args[i] = args[i].filter(function (item, pos, self) { return self.indexOf(item) == pos; }); } for (var i = 0; i < args.length; i++) { symmetricSet = compareArr(symmetricSet, args[i]); } return symmetricSet; } /****************************************************************** *compares two of the arrays and returns the symmetric set */ var compareArr = function (arr1, arr2) { var unique = []; //iterate through array 1 for (var i = arr1.length - 1; i >= 0; i--) { var posFoundArr2 = arr2.indexOf(arr1[i]); //if a number in arr1 is not matched up with a number in arr2 if (posFoundArr2 === -1) { //put it into the array named unique unique.push(arr1[i]); } else { //if it is matched up, remove it from both arrays arr2.splice(posFoundArr2, 1); arr1.splice(i, 1); } } //puts the numbers that were not matched from arr2 into the array named unique Array.prototype.push.apply(unique, arr2); //console.log(unique); return unique; }