Intermediate Algorithms - Diff two arrays

Intermediate Algorithms - Diff two arrays
0.0 0


I’m finally stumped. Been trying to wrap my head around this problem for a few days now and just can’t seem to figure it out. I also don’t want to go searching for the answer because I feel like that’s cheating. Does anyone have a hint that could point me in the right direction?


I used filters (and take a look at the optional parameters in the docs for .filter())



I just spent about 4 hours on this one - very hard!
Here’s my crazy solution:

function diffArray(arr1, arr2) {
  // Find elements that arr1 & arr2 have in common:
  var arr1SharedElementIndexes = [];
  var arr2SharedElementIndexes = [];
  for ( let i = 0, lgt = arr1.length; i < lgt; i++ ) {
  for ( let j = 0, lgt = arr2.length; j < lgt; j++ ) {
    if ( arr1[i] === arr2[j] ) {
  // Modify arr1 & arr2 by splicing common elements found above.
  // Insert zeros in lieu of spliced elements to preserve length of arrays
  for ( var i = 0, lgt = arr1SharedElementIndexes.length; i < lgt; i++ ) {
    arr1.splice(arr1SharedElementIndexes[i], 1, 0);
  for ( let j = 0, lgt = arr2SharedElementIndexes.length; j < lgt; j++ ) {
    arr2.splice(arr2SharedElementIndexes[j], 1, 0);
  // Concatenate modified arrays arr1 & arr2 
  var singleArrayShowingOutliers = arr1.concat(arr2);
  // Create a function to use for filtering out zeros
  var removeZeros = (x) => Boolean(x);
  return singleArrayShowingOutliers.filter(removeZeros);

// Test:
diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]); // should return ['pink wool']


@Soupedenuit - Your solution passes the tests FCC created, but your solution would not pass the following test, because you filter out zeros even if they are not in both arrays.

diffArray([0, 1], [2, 3]); // should return [0,1,2,3] but yours only returns [1,2,3]


Does anyone have a hint that could point me in the right direction?

Let’s say I got Arr3, which is the result of adding Arr1 and Arr2 together.

What happens if I kept every element in Arr3 that is unique for either Arr1 or Arr2?

####Some helpful Links:

Glad to hear that you did not give up,
Good luck.


That’s true, I used zeros to pass the test.
Can you suggest an alternative (using my solution - not a new solution) where I can still splice my arrays without losing index positions? Insert null perhaps, or NaN?


No matter if you insert null, NaN, or undefined with your splice and then filter out those values, you are still going to have a problem if any of those values were in one of the arrays and not the other,. Your solution would not include them when it should.

There may be a way to make it work with the way you are approaching the problem with splice, but honestly I would rethink your algorithm here. One way to approach this problem is two break it up into 2 pieces and then sew the 2 pieces together. The following is a possible algorithm:

  1. create a new array using a filter on arr1 which only keeps elements which are not in arr2
  2. create a new array using a filter on arr2 which only keeps elements which are not in arr1
  3. put these two new arrays together into an array called combined
  4. return the combined array

Note: For step 1 and 2 you could either use indexOf or includes on the applicable arrays to get the desired filter.


Thanks, I will try that later this week. I need a break from this one.


Am I missing a point on this challenge, I came up with a solution that seems much more straight-forward than what I’m seeing here and it passes all the tests, but now I fear something’s a miss.
I put my code in a codepen so as not to wrongly influence anyone here.

I’d appreciate the thoughts of anyone who has completed this challenge another way.


I always wonder if i should use temporary variables instead of just returning the answer but I really fancy as short as possible answer. I blurred the code to keep spoilers hidden:

function diffArray(arr1, arr2) { // Same, same; but different. return arr1.filter(x=> arr2.indexOf(x) == -1) .concat(arr2.filter(x=> arr1.indexOf(x) == -1)); }

It took some time to understand what i was missing before I entered the second line. Now I think it’s quite helpful to copy the test which didn’t pass and investigate its result :slight_smile: