Diff Two Arrays - how can i get this done simultaneously to avoid repetition?

Diff Two Arrays - how can i get this done simultaneously to avoid repetition?
0

#1

I thought I was on the code with this but I realised that the code will output the different elements in a new array TWICE…

Is there no function to check all elements of an array for multiple arrays and compare elements? I tried .includes , if that’s the right one can someone give me a pointer on how to fix this?

Your code so far


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

for (let i = 0 ; i<arr1.length ; i++) {


if ( (arr1.includes(arr2[i])) === false ) {

  newArr.push(arr2[i]);

  console.log(newArr);

} else if ( (arr2.includes(arr1[i])) === false ) {

  newArr.push(arr1[i]);

}


}



  return newArr;
}



console.log(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]));


Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays/


#2

What you are missing here is that the two arrays can be different lengths. You have a single for loop, so if arr1 is longer than arr2, then at some point in the following if statement, arr2[i] will be undefined. Since undefined will not be in arr1, you will end up pushing undefined into newArr.

if ((arr1.includes(arr2[i])) === false) {

#3

so an if statement to check if the lengths are not equal.

if (arr1.length !=arr2.length)

hmm… what I’m thinking atm is that if either is unequal, to extend the length by the difference of what their length is

But that would lead to empty/ null elements then we have the same problem in the end ultimately when all are compared, newArr will have null elements if both arrays do not share that :S


#4

A basic algorithm for this challenge would be:

  1. Iterate through arr1 and if any elements in arr1 are not in arr2, push them into a new array

  2. Iterate through arr2 and if any elements in arr2 are not in arr1, push them into the same new array created in step 1.

Is there repeated code with the above algorithm? Yes, so you could put the repeated code into a separate function and call that function twice.


#5

If you only want to iterate through a single array, then it will need to be the longest array. Then, in your for loop, when checking the if the longest array includes a specific element in the shortest array you will also need to make sure the element at the same index in the shortest array is not equal to undefined. This means you will need separate if statements and not an if/else if statement.


#6

This explanation is excellent, thank you.
This is my new attempt via converting your logic into code - it’s better than the other one but I’m still slipping somewhere. Today has been a long day so I’m sure I’ll get it tomorrow. Give pointers when you can thank you

function diffArray(arr1, arr2) {

var newArr = [];

if (arr1.length &gt; arr2.length)

{

for (let i = 0 ; i&lt;arr1.length ; i++) {

if ( (arr2.includes(arr1[i]) == false) &amp;&amp; ((arr2[i] !== undefined))) {

newArr.push(arr1[i]);

}

} } else {

for (let i = 0; i&lt;arr2.length; i++) {

if ( (arr1.includes(arr2[i]) == false) &amp;&amp; ((arr1[i] !== undefined)) ) {

if (arr1[i] != undefined) {

newArr.push(arr2[i]);

}

}

}

}

return newArr;

}

#7

Which algorithm suggestion are you trying to implement in your latest attempt? It seems you are trying to implement both in the same solution. Just pick one or the other.

Also, the code you posted has html special characters instead of literal symbols. For example &lt; instead of <. You should correct this or re-paste the code again.