Home > OS >  Find the intersecting elements of subarrays using nested for-loop
Find the intersecting elements of subarrays using nested for-loop

Time:10-22

i am trying to find the intersection of the elements using nested for loop. I made several mistakes doing so.

  1. I dont know why arrOfArrs[i 1] is not applicable
  2. I dont understand how will i compare the resulting array to the third array after the first iteration

Hypothetically, if the code had worked, wouldnt it just compare array1 to array2 push the result into resultArray, and same thing for array2 to array3 and finally push the result.

Please share your insight on this. Much obliged!

function intersection(arrOfArrs){
  
  let resultArray = [];
  let nextArray = 0;
  
  for(let i = 0 ; i < arrOfArrs.length; i   ){
    
    for(let j = 0; j < arrOfArrs[i].length; j  ){
        const currEl = arrOfArrs[i][j];
      
        if(arrOfArrs[(i 1)].length){
        
        if(arrOfArrs[(i 1)].includes(currEl)){
        resultArray.push(currEl);
        }
      }
    }
  }
  return resultArray;
}
    
const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log(intersection([arr1, arr2, arr3])); // should log: [5, 15]

CodePudding user response:

Iterate each item in each array over the iteration of each item in the next array, if there is one, like:

const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log(intersection([arr1, arr2, arr3])); // should log: [5, 15]

function intersection(arrays){
  var result = [];
  for (var i = 0; i < arrays.length; i  ) { // for each array
    var placeholder = null;
    for (var ii = 0; ii < arrays[i].length; ii  ) { // for each item in each array
      if(arrays[i 1]){ // if there is another array to inspect
        for (var iii = 0; iii < arrays[i 1].length; iii  ) { // for each item in the next array compare to this item
          if(arrays[i][ii] === arrays[i 1][iii]){ // if this item matches any item in the next array then store it.
            placeholder = arrays[i 1][iii];
          }
        }
      }
    }
    if (placeholder){
      result.push(placeholder);
    }
  }
  return result;
}

CodePudding user response:

I did it without a recursion but did use a helper function to compare 2 arrays.

function intersection(arrOfArrs) {
  if (arrOfArrs.length == 0) {
    return []
  }
  var resultArray = arrOfArrs[0];
  for (let i = 1; i < arrOfArrs.length; i  ) {
    resultArray = my_intersection(resultArray, arrOfArrs[i])
  }
  return resultArray;
}

const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log(intersection([arr1, arr2, arr3]));


function my_intersection(array1, array2) {
  // from https://stackoverflow.com/a/1885569/3807365
  // return filteredArray = array1.filter(value => array2.includes(value));

  var helper = {}
  var result = [];
  for (var i = 0; i < array1.length; i  ) {
    helper[array1[i]] = 1;
  }
  for (var i = 0; i < array2.length; i  ) {
    if (helper[array2[i]]) {
      result.push(array2[i])
    }
  }
  return result;
}

CodePudding user response:

You could use a Set created from one array and repeatedly filter() against it for successive arrays.

Iteratively returning an array:

function intersection([arr, ...rest]) {
  let result = new Set(arr);

  for (const arr of rest) {
    result = new Set(arr.filter(n => result.has(n)))
  }

  return Array.from(result)
}

const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log(intersection([arr1, arr2, arr3])); // should log: [5, 15]

Recursively returning a Set:

function intersection([arr, ...rest]) {
  return rest.length
    ? new Set(((m) => arr.filter(n => m.has(n)))(intersection(rest)))
    : new Set(arr);
}

const arr1 = [5, 10, 15, 20];
const arr2 = [15, 88, 1, 5, 7];
const arr3 = [1, 10, 15, 5, 20];
console.log([...intersection([arr1, arr2, arr3])]); // should log: [5, 15]

  • Related