Want optimised solution

Want optimised solution
0.0 0

#1

i've solved this problem like this i would like to know how to solve it using map or filter or reduce functions.

js
function diffArray(arr1, arr2) {
  var Arr1 = [];
  var Arr2 = [];
  let proceed;
  for (let i=0;i<arr1.length;i++) {
    for (let j=0;j<=arr2.length;j++) {
      if (arr1[i] == arr2[j]) {
        proceed = false;
        break;
      }
      else proceed = true;
    }
    if (proceed) {
      Arr1.push(arr1[i])
    }
  }
    for (let i=0;i<arr2.length;i++) {
    for (let j=0;j<=arr1.length;j++) {
      if (arr1[j] == arr2[i]) {
        proceed = false;
        break;
      }
      else proceed = true;
    }
    if (proceed) {
      Arr1.push(arr2[i])
    }
  }

  return Arr1;
}

console.log(diffArray( ["snuffleupagus", "cookie monster", "elmo"],["a"]));

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


#2

Hi @coderharshit

This is one way you could do it with filter:

function diffArray(arr1, arr2) {
  return arr1.concat(arr2)
    .filter((x, y, arr) => arr.filter(a => a === x).length === 1)
}

Basically, it combines both arrays with concat then filters out all of the elements that are duplicated in the combined array.


#3

@coderharshit, @joesmith100 - Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#4

what is the use of .length === 1?


#5

Basically checks that only one instance exists in the array and thus isn’t duplicated.


#6

actually i m little confused here is function:

function diffArray(arr1, arr2) {
return arr1.concat(arr2)
.filter((x, y, arr) => arr.filter(a => a === x).length === 1)
}

arr1.concat(arr2) ->concat the arr1 and arr2
.filter(x,y,arr) -> x is the every element of concatenated array , y is index , arr is name of concatenated array?
=>arr.filter(a=>a===x) ->this will check if a === x it will return true
.length->?

please tell where m i wrong?


#7

arr references the array being filtered, which in this case will be the combined array. See here

So when we do arr.filter(a=>a===x).length === 1. What we’re saying is, make a new array with only the elements that match x variable, i.e the current element being checked. If the length of that array is 1, i.e. only one of those elements exists in the combined array, then included it in the outer filtered array.

Here’s an easy example:

const arr1 = [1];
const arr2 = [1, 2];

arr1.concat(arr2) // [1, 1, 2]
    // loop 1
    .filter((x, y, arr) => // x = 1, y = 0, arr = [1,1,2]
        arr.filter(a => a === x) // return = [1, 1]
           .length === 1 // return = false. Element not included in final array
    // loop 2
    .filter((x, y, arr) => // x = 1, y = 1, arr = [1,1,2]
        arr.filter(a => a === x) // return = [1, 1]
           .length === 1 // return = false. Element not included in final array
    // loop 3
    .filter((x, y, arr) => // x = 2, y = 2, arr = [1,1,2]
        arr.filter(a => a === x) // return = [2]
           .length === 1 // return = true. Element included in final array

So the only element that passes is 2 because it is only in arr2 and when combined with arr1, won’t be “duplicated”.

Does that make sense?


#8

thank you so much for your time .this made me very clear whats going on :star_struck: