Skip to main content

JavaScript Function For Symmetric Difference

EDIT: Note that the code below works (at least as far as I know) so long as you assume each array is formatted like a set.  If either of the arrays have repeating entries -- i.e. behave the way arrays are allowed to behave -- the algorithm won't work and is seriously in error.  Oh well -- back to the drawing board.

The symmetric difference of two sets is the difference between their union and their intersection. Consider the following sets:
A = {1, 2, 3, 4, 5}
B = {1, 3, 5, 7, 9} 
The union of A and B is
{1, 2, 3, 4, 5, 7, 9}.
The intersection of A and B is
{1, 3, 5}.
So the symmetric difference of A and B is
{1, 2, 3, 4, 5, 7, 9} - {1, 3, 5} = {2, 4, 7, 9}.
My job was to construct a two argument JavaScript function that would correctly return this result.  I decided to proceed as follows:
1. Concatenate the two arrays to produce their "union" (with duplicate values).
2. Remove any value that has a duplicate.
Here is my code:
function diffArray(arr1, arr2) {
  var newArr = arr1.concat(arr2);
  newArr = newArr.filter(function (value, index, array) {
                elOut = array.slice(0,index).concat(array.slice(index+1,array.length));
  return elOut.indexOf(value) == -1;
                });
  return newArr;
}
The function diffArray() takes two arrays as arguments (no debugging for non-array inputs).  It concatenates them together as newArr.  Then it runs newArr through the function filter().  From what I can tell, this function is going through each element of the newArr and returning it if and only if it passes a Boolean test.  Calling the function accordingly gives me three variables to work with: the index of the array being examined, the value of that index, and the array itself.

So what I wanted to do was consider whether, for a given item in the array, a duplicate of that item was present in the array.  Using the method indexOf(), I knew I could search an array and get the position of a given value.  The problem was that I needed the index of the duplicate of the value I was searching for and not the value itself -- searching in newArr wouldn't work.  The solution was to take the value I was searching for out of the array.  To do so, I sliced the array on the index of that value, forming a new array, elOut ("Get the element out!")  I then had the callback return False (-1) if the value was still present in elOut.  Reconstructing newArr to include only those elements that passed the test, I was then able to fulfill the requirements of the assignment.

Comments

Popular posts from this blog

Getting Geodata From Google's API

The apps I'm going to be analyzing are part of Dr. Charles Severance's MOOC on Python and Databases and work together according to the following structure (which applies both in this specific case and more generally to any application that creates and interprets a database using online data). The data source, in this case, is Google's Google Maps Geocoding API.  The "package" has two components: geoload.py  and geodump.py .  geoload.py  reads a list of locations from a file -- addresses for which we would like geographical information -- requests information about them from Google, and stores the information on a database ( geodata.db ).  geodump.py  reads and parses data from the database in JSON, then loads that into a javascript file.  The javascript is then used to create a web page on which the data is visualized as a series of points on the world-map.  Dr. Severance's course focuses on Python, so I'm only going to work my way through ...

Compiling and Executing Java Files With -cp

I decided I was going to "man up" and figure out how to compile a java program with an external dependency from the command line instead of relying on an IDE-- the DOS command line, to be more specific. I ran into a few problems: 1.  The external dependency was given to me as a java file.  I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class. 2.  When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE.  The Internet told me to check my path variable for Java.  It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler).  I asked the Internet again and found the following command: for %I in (java.exe) do @echo %~$PATH:I I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" com...

Quick Find / Quick Union (Connected Nodes)

Setup This week I learned about the "Quick Find" or "Quick Union" algorithm. Imagine an NxN grid of nodes, some of which are connected by lines. A connection can be interpreted as accessibility: if two nodes are connected, you can get from one to the other. Every node is accessible to itself: to get where you already are, stay there. Also, If you can get from A to B, you can go back from B to A. And if you can get from A to B and from B to C, then you can get from A to C. As a consequence, the connection between nodes divides the grid into regions of mutually accessible nodes. You can travel from any node in a given region to any other node in that region -- but not to any nodes outside that region (exercise to reader -- proof by contradiction). The problem has two parts. First, find a way to represent this grid structure and the accessibility relation; second, use your schema to efficiently calculate whether two given nodes are accessible to each other. ...