Intermediate Algorithms - Diff two arrays

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())

1 Like

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.

1 Like

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:

1 Like

here are my Two workarounds :slight_smile:

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  // the loop loops around arr1 
  for (let i=0;i<arr1.length;i++){
      //if the condition is true  I push the inexistent element  to newArr 
  //other loop for arr2 
    for(let j=0;j<arr2.length;j++){
//if the condition is true  I push the inexistent element  to newArr 
return newArr;

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

//another approach for this challenge using filter method 
function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  // I am filtering arr1
let filterArr1= arr1.filter(function(item){
    return arr2.indexOf(item)===-1;
  // I am filtering arr2
let filterArr2=arr2.filter(function(value){
    return arr1.indexOf(value)===-1;
// concat the two filtered arrays into newArr;
  return newArr.concat(filterArr1,filterArr2);

diffArray([1, 2, 7, 5], [1, 2, 3, 4, 5]);

Not the most elegant solution but makes sense to me.

function diffArray(arr1, arr2) {
    let tempArr = arr1.concat(arr2); // join to make single array

    let newArr = []; // 
    let temp = 0;
    let i = 0;

    while (i < (tempArr.length)) { // going through each element...
        temp = tempArr.filter(x => x == tempArr[i]); // return temp only containing tempArr[i]
        if(temp.length == 1){
            newArr.push(temp[0]); // push to newArr if temp only has 1 item

    return newArr;

diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]);

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, focus on trying to answer the original poster’s questions instead of posting a full working solution just for the sake of it.

Thank you.