dadadodo dadadodo - 1 month ago 18
Javascript Question

compare elements of array of many arrays

I trying to loop through an array of arrays, and compare the elements with each-other in-order to find the common elements. so lets say if we have

var arr = [[1,2,3],[4,2,5]];
I want to first compare [i][i] and [i+1][i], [i][i+1] and [i+1][i+1] and [i][i+2] and [i+1][i+2] and so on. here is my code:

function sym(args) {
var fullArr = [];
var finalArr = [];
// store the arguments inside a single array
for (var count = 0; count < arguments.length; count ++) {
fullArr[count] = arguments[count];
}
// loop through finalArr[];
for (var i = 0; i < fullArr.length; i++) {
if (fullArr[i][i] == fullArr[i++][i++]) {
// if the element matches (it is a common element)
// store it inside finalArr
finalArr[i] = fullArr[i];
}
}
return finalArr;
}

sym([1, 2, 3], [5, 2, 1, 4]);


problem: when I run the code instead of an array containing the matching element, I get an empty array

Answer

You first have to iterate over one array and see if the other array includes the value you have specified.

My answer is similar to a nested for loop in that the includes method does exactly that. It takes in as a parameter an element and checks if the array which called it contains said element. In order to do that it must iterate through all elements in the array in the worst case.

My answer also assumes that you only want to count duplicate matches once.

function sym(args) {
  var fullArr = [];
  var finalArr = [];
  // store the arguments inside a single array
  for (var count = 0; count < arguments.length; count ++) {
    fullArr[count] = arguments[count];
  }
  // loop through finalArr[];
  
  //since you are comparing only two arrays in this 
 //example you just have to iterate over each element in the first array aka fullArr[0] and
 //check if each element "e" is also in the second array aka fullArr[1] 
 //AND that your final output array does not already contain it.
 //If both of these are true then we push the element to the output array.
  fullArr[0].forEach(function(e){
       if(fullArr[1].includes(e) && !finalArr.includes(e)) finalArr.push(e);
    
    });
  return finalArr;
}

sym([1, 2, 3], [5, 2, 1, 4]);

However if you want to check if a particular element exists in all collections of an n length array then I would propose something like this:

function sym(args) {
  var fullArr = [];
  var finalArr = [];
  // store the arguments inside a single array
  for (var count = 0; count < arguments.length; count ++) {
    fullArr[count] = arguments[count];
  }
  // loop through finalArr[];
 
  fullArr.forEach(function(e){
      
      finalArr = finalArr.concat( e.filter( function(e1) {
         if(finalArr.indexOf(e1) < 0){
             return fullArr.every( function(arr){
                   return arr.indexOf(e1) > -1;
             })   
           }else{
                   return false;
           };
     }));
  }); 
  alert(finalArr);        
  return finalArr;
}

sym([1, 2, 3,4], [5, 2, 1, 4], [4,1,2, 5]);