0

In my code I check if there is similar values in two arrays and if there are similar values they wont be displayed in my result, however if I do the same check but switch the arrays length, which means Array2 is longer than Array1 , I got an empty result, empty array. How can I get the same result even after switching the lenght of the arrays?

My code :

var Array2 = [ "1", "2", "3", "4", "5" ]; 
var Array1 = ["1","2","3"];
      
var result = [];
      
for(var i = 0; i < Array1.length ; i++){      
  var x = Array1[i];
  var check = false;        
      
  for( var y = 0; y < Array2.length; y++){         
    if(x == Array2[y]){
      check = true;
    }
  }
  if(!check){
    result.push(x);
  }
}
console.log(result);
Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
Ilona Semyonov
  • 73
  • 1
  • 10
  • 2
    Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Alexander Lallier Jul 11 '18 at 12:43
  • You can try https://lodash.com/docs/4.17.10#difference – Harish Jul 11 '18 at 12:44
  • 1
    Every element in the shorter array is found in the larger array, but not every element in the longer array is found in the shorter array. Your code is doing exactly what it's written to do. – Pointy Jul 11 '18 at 12:46
  • I'm voting to close this question as off-topic because the code does exactly what you want it to do. – Jonas Wilms Jul 11 '18 at 13:00

3 Answers3

4

You can use ES6 .filter() method to remove duplicates from arrays.

ES6 Approach:


let Array2 = [ "1", "2", "3", "4", "5" ]; 
let Array1 = ["1","2","3"];

let result = Array2.filter(val => !Array1.includes(val));

console.log(result);

Vanilla JS Approach:


let Array1 = ["1", "2", "3"];
let Array2 = ["1", "2", "3", "4", "5"];

function getMissing(a, b) {
  var missings = [];
  var matches = false;

  for (let i = 0; i < a.length; i++) {
    matches = false;
    for (let e = 0; e < b.length; e++) {
      if (a[i] === b[e]) matches = true;
    }
    if (!matches) missings.push(a[i]);
  }
  return missings;
}

console.log(getMissing(Array2, Array1));

jQuery Approach:


let Array1 = ["1", "2", "3"];
let Array2 = ["1", "2", "3", "4", "5"];

let difference = $(Array2).not(Array1).get();

console.log(difference);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Alex
  • 2,164
  • 1
  • 9
  • 27
2

Try following

var Array2 = [ "1", "2", "3", "4", "5" ]; 
var Array1 = ["1","2","3"];

var result = [...Array1.filter(v => !Array2.includes(v)), ...Array2.filter(v => !Array1.includes(v))];
console.log(result);

Can improve the performance using following approach

var Array2 = [ "1", "2", "3", "4", "5" ]; 
var Array1 = ["1","2","3"];

var map2 = Array2.reduce((a,c) => Object.assign(a, {[c]:true}), {});
var map1 = Array1.reduce((a,c) => Object.assign(a, {[c]:true}), {});

var result = [...Object.keys(map2).filter(k => !map1[k]), ...Object.keys(map1).filter(k => !map2[k])];
console.log(result);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • Your first example works partially ,it prints the result as values '4' and '5' if Array2 is bigger than 1, but if you switch the arrays and Array 1 is bigger now than two it prints '4','5','4','5' – Ilona Semyonov Jul 11 '18 at 13:08
  • @IlonaSemyonov - The above answer does not worry about the length of the arrays. Can you please present the case where it did not working for you? – Nikhil Aggarwal Jul 11 '18 at 14:27
0

Eloborate way

var Array2 = [ "1", "2", "3", "4", "5" ]; 
var Array1 = ["1","2","3"];
var result = [];
var matches = false;

if(Array2.length >= Array1.length){
  getUnique(Array2,Array1);
}
else{
  getUnique(Array1,Array2);
}


function getUnique(maxArray,minArray){
  $.each(maxArray,function(n1,i1){
     matches = false;
     $.each(minArray,function(n2,i2){
           if(i1 === i2){
             matches = true;
            }          
      });
      if(!matches){
        result.push(i1);
      }
  });
}
console.log(result);
Diwa
  • 73
  • 1
  • 7