Help with Diff Two Arrays algorithm

I’ve been working on this algorithm for a while now, and I am struck.

My thinking has been: compare the items from arr1 and arr2 to see which one does not match and return it. Here’s an example of what I’ve done:

for(let i = 0; i < arr1.length; i++) {
  for(let j = 0; j < arr2.length; j++) {
if(arr1[i] !== arr2[j])
  };
  };

Of course, I’m going to get back a Boolean value. I’ve been trying to return the item itself but nothing.

I’ve also sent all items into newArr and tried to find a method to return the items that is different, like:

const map1 = arr1.map(x => arr1 === arr2);
      console.log(map1)

or

const found = newArr.find(element => element === false)

I think my mind is stuck in a way of looking at this. Can anyone give me a hint as to what is afoul. Thanks.

There is nothing inside of the if statement, so I don’t know what this code is intended to do.

This says arr1 === arr2, or “array one is exactly the same array in memory as array two”, which is never true in this problem.

Here you are checking if the element is exactly equal to the boolean literal value false.


Lets back up for a second here.

Lets consider arr1 = [1, 2, 5], arr2 =[1, 2, 3, 4]. What is the diff of these two arrays? How would you find that diff, step by step, using pencil and paper (no JS syntax!!)

IF items from the same index do not match,
THEN send that item into a new array
IF the following item that now finds itself on that index does not match with its counterpart,
THEN send that item to a new array, as well

Hmm, I’ve almost finished the basic algorithms (one left to go), but I feel like this is beyond what I can currently do.

What is this business about ‘same index’? The diff says nothing about indices. (And what is an ‘index’ if you are talking about pencil and paper?)

I’m not sure you understand what the diff is?

I read it as different, but I’m sure that that is not it.

What do you mean by ‘different’. That is not a full definition.

Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.

What is the diff for these two arrays:

As per Wikipedia, "the symmetric difference of two sets, also known as the disjunctive union, is the set of elements which are in either of the sets, but not in their intersection. For example, the symmetric difference of the sets "

Dude…

Do you know how to make the answer without JS? If you don’t, then this problem is impossible with JS.

the diff is 3, 4 and 5

Fantastic. How did you know?

Because 1 and 2 are found in both arrays.

Ok, so now lets think about code.

How do you express the condition “is elem in arr2”?

Yep, includes method

I am sorry, but I just don’t understand your replies to my questions. I don’t think we are making any sense to each other. Hopefully someone else can help you.

Sorry @JeremyLT , I think I got very confused. I came up with some code that is close to the answer, I believe:

function diffArray(arr1, arr2) {
  const newArr = [];
  for(let i = 0; i < arr1.length; i++) {  
    if(!arr2.includes(arr1[i])){
    newArr.push(arr2[i])
    }
    }
    for(let j = 0; j < arr2.length; j++) {  
    if(!arr1.includes(arr2[j])){
    newArr.push(arr2[j])
  };
  }
  console.log(newArr)
  return newArr;
}

Okay

function diffArray(arr1, arr2) {
  const newArr = [];
  for(let i = 0; i < arr1.length; i++) {  
    if(!arr2.includes(arr1[i])){
    newArr.push(arr1[i]) <= arr1
    }
    }
    for(let j = 0; j < arr2.length; j++) {  
    if(!arr1.includes(arr2[j])){
    newArr.push(arr2[j])
  };
  }
  console.log(newArr)
  return newArr;
}

Consistent formatting:

function diffArray(arr1, arr2) {
  const newArr = [];
  for (let i = 0; i < arr1.length; i++) {  
    if (!arr2.includes(arr1[i])) {
      newArr.push(arr1[i]) <= arr1
    }
  }
  for (let j = 0; j < arr2.length; j++) {  
    if (!arr1.includes(arr2[j])) {
      newArr.push(arr2[j])
    }
  }
  console.log(newArr)
  return newArr;
}

Thanks for the help; I really made a big mess. I believe the problem was that I did not understand what the exercise was asking in the first place. I had an algorithm in my head, and for some reason I could not let go. So I thought, “I have to achieve this,” which lead me astray. Like you said, “what is diff?” and the problem is asking for symmetric difference. After I looked it up on Wikipedia, I could understand what it was. Funny because one of the first tips I got on FCC was: “understand the problem first!” Ai ai.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.