Diff two arrays algorithm question

hey guys,
my idea was to join the arrays and then filter everything that is duplicated.
I basically did but then I am left with an original array… any idea how I can remove the doubled numbers?

edit: I have edited my code got new stuff in.
now i find all the included numbers but I filter out what I need is there a way to get that filtered number some how?

  **Your code so far**

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

  const arr3 = arr1.concat(arr2);
  const filtered = arr3.filter((num) => {
       if (arr1.includes(num) && arr2.includes(num) == false) {
      return num


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/99.0.4844.51 Safari/537.36

Challenge: Diff Two Arrays

Link to the challenge:

You are definitely on the right track here. Your filter function just needs a little work. Reread the instructions again:

“return a new array with any items only found in one of the two given arrays, but not both.”

You have the two original arrays (arr1 and arr2). You only want items in the new array that are not in both of the original arrays. What is another way to rephrase this requirement?

1 Like

I am not sure if you saw the edit but I got this. so this code marks the numbers in both of the arrays but I can’t seem to write the next code to remove them.

Yep, you are closer. How does the filter method work on an array. You need to understand that in order to make this work.

filter passes whatever the callback function passes as true, that’s why im getting exactly what i dont need with my code, but i tried the opposite and giving both of them == false.
not sure why it gives an empty array if i give both of them == false.

Right. So you only want your callback function to return true when you want to keep a number. Otherwise, what do you want it to return?

    if (arr1.includes(num) && arr2.includes(num) == false) {
      return num

shouldn’t this pass the num that isn’t in both arrays?

This isn’t doing what you think it is doing.

But that’s not what the callback function should be returning. Are you sure you understand how filter works?

loops the array and then applies the condition of the callback function on each loop, if the function passes it returns it.

But how does the filter know that the element 'passes the callback function?

Not sure I understand the question.
isn’t filter a built in function inside array object?

Right, it is. But how does it work? What does the callback function need to do for elements that should be retained? What should the callback function do for elements that should be filtered out.

If the output is not a boolean, the value is coerced. That isn’t what you want here.

for elements to retain it should pass true, what ever isn’t passed it is filtered out

Are you returning true or false?

ty! I have passed it ;p

1 Like

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