0

I have a problem needing two arrays containing some similar values and different values. I need to concat the arrays into a new array and remove the similar values only showing the individual values. something like arr1 = [1, 44, 2, 3, 5], arr2 = [33, 1, 2, 3, 4, 5], arr3 = [], return arr3 [44, 33, 4]. I have tried a few different ways with no success, one using a nested for loop and the other using .filter(). Any thoughts on how I can solve this? Here is my code:

const arrayDiffs = (arr1, arr2) => {
  let arr3 = [];

  for (let i = 0; i < arr1.length; i++) {
    if (arr3.indexOf(arr1[i]) === -1) {
      arr3.push(arr1[1]);
    }
    for (let n = 0; n < arr2.length; n++) {
      if (arr3.indexOf(arr2[n]) === -1) {
        arr3.push(arr2[n]);
      }
    }
  return arr3;
  };  
}
console.log(arrayDiffs([1, 44, 2, 3, 5], [33, 1, 2, 3, 4, 5]));

I have also tried this way:

let arr3 = [];
const arrayDiffs = (arr1, arr2) => {

  arr3 = arr1.concat(arr2);
  arr3 = arr3.filter(function(item, index) {
    if(arr3.indexOf(item) == index){
      return true;
    }
    return false;
  });
  
}
console.log(arrayDiffs([1, 44, 2, 3, 5], [33, 1, 2, 3, 4, 5]));
Alx_Wil95
  • 97
  • 1
  • 2
  • 9
  • Does this answer your question? [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – poPaTheGuru Aug 26 '21 at 04:21
  • No, I'd still need to remove "singh", in this case, from the entire array – Alx_Wil95 Aug 26 '21 at 04:50

4 Answers4

2

const myFunc = (a,b) => {
  const a_but_not_b = a.filter(x=>!b.includes(x));
  const b_but_not_a = b.filter(x=>!a.includes(x));
  return [...a_but_not_b,...b_but_not_a];
}

console.log(myFunc([1,2,3],[2,3,4]));

But, let me explain more:

  1. Use filter and includes to get difference.
  2. Last I concat the arrays using spread operator [...a,...b].
Iceman
  • 6,035
  • 2
  • 23
  • 34
  • I understand the concept, but I would only need it to return the different values "[44, 33, 4]" removing the [1, 2, 3, 5] entirely. Could I use this method to get that result? – Alx_Wil95 Aug 26 '21 at 04:33
  • @Alx_Wil95 check now – Iceman Aug 26 '21 at 04:54
  • I really appreciate the help! – Alx_Wil95 Aug 26 '21 at 04:54
  • 1
    I'm new to js so let me see if I'm getting this right. basically you create a new function where filter() finds a and uses it to create a new function from anything that's not b then vice versa finding b and creating a new function that's not a and then you concat them with the spread operator? – Alx_Wil95 Aug 26 '21 at 05:13
  • 1
    `A.filter(fn(x))` will run function `fn` once for each element in the array and pass the element to the fn. if the fn returned true, then its collected, else dropped. This final array is then returned to the left of the `=` operator. I've made teh variable names, such that its easy for you to understand. – Iceman Aug 26 '21 at 05:35
1

Using your first method only we can achieve this. You have to do the following modifications.

for (let i = 0; i < arr1.length; i++) {
  if (arr2.indexOf(arr1[i]) === -1) { // first compare the value with arr2 and arr1 and push the non-available values into arr3
    arr3.push(arr1[i]);
  }
}
for (let n = 0; n < arr2.length; n++) {
  if (arr1.indexOf(arr2[n]) === -1) {  //compare the value with arr1 and arr2 and push the non-available values into arr3
    arr3.push(arr2[n]);
  }
}

const arrayDiffs = (arr1, arr2) => {
  let arr3 = [];

  for (let i = 0; i < arr1.length; i++) {
    if (arr2.indexOf(arr1[i]) === -1) {
      arr3.push(arr1[i]);
    }
  }
  for (let n = 0; n < arr2.length; n++) {
      if (arr1.indexOf(arr2[n]) === -1) {
        arr3.push(arr2[n]);
      }
  }
  return arr3;   
}
console.log(arrayDiffs([1, 44, 2, 3, 5], [33, 1, 2, 3, 4, 5]));
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54
1
[...arr1,...arr2].filter(e=>!(arr1.includes(e)&&arr2.includes(e)))

var arr1 = [1, 44, 2, 3, 5],
arr2 = [33, 1, 2, 3, 4, 5],
arr3 = [...arr1,...arr2]
.filter(e=>!(arr1.includes(e)&&arr2.includes(e)));
console.log(arr3);
codelion
  • 175
  • 1
  • 6
0

Have you tried sets in javassript. I think they are used for storing only unique elements.this will bring down you complexity to O(N), where N is total number of elements in arrays. Example :

const letters = new Set()

Freez
  • 53
  • 9
  • @VisioN I dont think so thats the case in javascript `const s = new Set(); s.add("q"); s.add("x"); s.add("c"); s.add("q"); // duplicate for (const v of s) { console.log(v); // q, x, c -- insertion order }` Try running the above code. You will see javascript set maintains order – Freez Aug 26 '21 at 07:06
  • You are right. Removed my erroneous comment. – VisioN Aug 31 '21 at 10:46