Diff Two Arrays (cannot run 2 nested-for-loops)

Tell us what’s happening:
I am trying to splice off common elements in the each of the arrays, and then concatenate them to get the symmetric difference of the two arrays.

However, I discovered that my code is unable to run the second nested for-loop to get my desired result. The problem goes away when I comment out the first nested for-loop.

Your code so far


function diffArray(arr1, arr2) {
var longerArray = [];
var shorterArray = [];

if (arr1.length > arr2.length){
 longerArray = arr1;
 shorterArray = arr2;
} else {
 longerArray = arr2;
 shorterArray = arr1;
}

//finding A' - 1st nested for-loop
for(let i = 0; i < longerArray.length; i++){
  for(let j = 0; j < shorterArray.length; j++){
    if(longerArray[i] === shorterArray[j]){
      longerArray.splice(i,1)
      console.log(longerArray);
    } 
  }
}

console.log(longerArray);

//finding B' -2nd nested for-loop
for(let x = 0; x < shorterArray.length; x++){
  for(let y = 0; y < longerArray.length; y++){
    if(shorterArray[x] === longerArray[y]){
      shorterArray.splice(x,1)
      console.log(shorterArray); 
    } 
  } 
} 

console.log(shorterArray); 

  var newArr = [];
  newArr = longerArray.concat(shorterArray);
console.log(newArr);
  
  // Same, same; but different.
  return newArr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36.

Link to the challenge:

I have managed to solve the problem in the following, but I am still unsure why my initial method would not work.

function diffArray(arr1, arr2) {
var longerArray = [];
var shorterArray = [];

if (arr1.length > arr2.length){
 longerArray = arr1;
 shorterArray = arr2;
} else {
 longerArray = arr2;
 shorterArray = arr1;
}

//finding A'
for(let i = 0; i < longerArray.length; i++){
  for(let j = 0; j < shorterArray.length; j++){
    if(longerArray[i] === shorterArray[j]){
      longerArray.splice(i,1);
      shorterArray.splice(j,1);
      console.log(longerArray);
      console.log(shorterArray);
      j--;
    } 
  }
}

console.log(longerArray);
console.log(shorterArray);

  var newArr = [];
  newArr = longerArray.concat(shorterArray);
  console.log(newArr);
  
  // Same, same; but different.
  return newArr;
}

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

@cornstar94

I see what you’re trying to do in your first solution. Keep in mind that Array.splice() mutates, meaning it will change the array when called. As a result, longerArray will only contain unique elements after the first nested for-loop. Then when you use it to match elements of shorterArray, nothing from shorterArray will be removed! Because nested for-loop goes through every possible pair, you will only need one as I see you have already figured out in your new solution.

1 Like

That’s a splendid explanation! I guess my mind was foggy when I was doing this. Thank you!