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] ) {
        arr1SharedElementIndexes.push(i);
        arr2SharedElementIndexes.push(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']

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:
Array.prototype.indexOf()
Array.prototype.filter()

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?

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 http://codepen.io/therichardnolan/pen/qrGqRO 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 
     if((arr2.indexOf(arr1[i]))===-1){
           newArr.push(arr1[i]);  
     }   
  }
  
  //other loop for arr2 
    for(let j=0;j<arr2.length;j++){
//if the condition is true  I push the inexistent element  to newArr 
        if((arr1.indexOf(arr2[j]))===-1){
             newArr.push(arr2[j]);
}
}
  
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
        }
        i++;
    }

    console.log(newArr);
    return newArr;
}

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