0

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]
  • 1
    Your **includes** is missing an argument. You will need to pass the value that you are looking for. Also i+1 will error out on the LAST element as i+1 doesn't exist – imvain2 Oct 21 '22 at 20:28
  • i tried fixing it by using an if statement, but idk how to proceed further – Anatolian Rocks Oct 21 '22 at 20:39

3 Answers3

0

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;
}
IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • This is a great solution! I can truly learn more about the helper functions. I would like to find it without using array methods, and using helper functions. Thank you for your effort! – Anatolian Rocks Oct 21 '22 at 20:48
  • I have a more efficient method for the helper function and "original" i'll edit my question – IT goldman Oct 21 '22 at 20:49
0

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]

Nested, inline with your actual question about nested loops, here is a nested implementation of the above.

function intersection(arr) {
  let [res, ...rest] = arr;

  for (const a of rest) {
    const temp = [];
    for (const b of a) {
      for (const c of res) {
        if (c === b) {
          temp.push(b);
          break;
        }
      }
    }
    res = temp;
  }

  return res;
}

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

All of the above yield an array of unique intersecting elements not including duplicates:

intersection([[1, 2, 3, 3],[1, 2, 3, 3],[1, 2, 3, 3]]) //-> [1, 2, 3]

but it occurs to me that possibly you're looking for the complete intersection:

intersection([[1, 2, 3, 3],[1, 2, 3, 3],[1, 2, 3, 3]]) // [1, 2, 3, 3]

in which case you'll need to account for each intersection, not just general inclusion in the previous array.

function intersection(arr) {
  let [[...res], ...rest] = arr;

  for (const a of rest) {
    const temp = [];
    for (const b of a) {
      for (const [i, c] of res.entries()) {
        if (c === b) {
          temp.push(b);
          res.splice(i, 1);
          break;
        }
      }
    }
    res = temp;
  }

  return res;
}

console.log(intersection([[3, 1, 2, 3], [2, 1, 3, 3], [1, 2, 3, 3]])); 
// [1, 2, 3, 3]
pilchard
  • 12,414
  • 5
  • 11
  • 23
0

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;
}
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • This doesn't yield consistent or complete results for other inputs. `intersection([[1, 2, 3], [1, 2, 3], [1, 2, 3]])` -> `[3,3]` – pilchard Oct 22 '22 at 10:16