简体   繁体   中英

find intersection elements in arrays in an array

I need to construct a function intersection that compares input arrays and returns a new array with elements found in all of the inputs. The following solution works if in each array the numbers only repeat once, otherwise it breaks. Also, I don't know how to simplify and not use messy for loops:

function intersection(arrayOfArrays) {
  let joinedArray = [];
  let reducedArray = [];
  for (let iOuter in arrayOfArrays) {
    for (let iInner in arrayOfArrays[iOuter]) {
      joinedArray.push(arrayOfArrays[iOuter][iInner]);
    }
    return joinedArray;
  }


  for (let i in joinedArray.sort()) {
    if (joinedArray[i] === joinedArray[ i - (arrayOfArrays.length - 1)]) {
      reducedArray.push(joinedArray[i]);
    }
  }
  return reducedArray;
}

Try thhis:-

function a1(ar,ar1){
    x = new Set(ar)
    y = new Set(ar1)
    var result = [] 
    for (let i of x){
        if (y.has(i)){
            result.push(i)
        }
    }
    if (result){return result}
    else{ return 0}
}
var a= [3,4,5,6]
var b = [8,5,6,1]
console.log(a1(a,b)) //output=> [5,6]

Hopefully this snippet will be useful

 var a = [2, 3, 9]; var b = [2, 8, 9, 4, 1]; var c = [3, 4, 5, 1, 2, 1, 9]; var d = [1, 2] function intersect() { // create an empty array to store any input array,All the comparasion // will be done against this one var initialArray = []; // Convert all the arguments object to array // there can be n number of supplied input array // sorting the array by it's length. the shortest array //will have at least all the elements var x = Array.prototype.slice.call(arguments).sort(function(a, b) { return a.length - b.length }); initialArray = x[0]; // loop over remaining array for (var i = 1; i < x.length; i++) { var tempArray = x[i]; // now check if every element of the initial array is present // in rest of the arrays initialArray.forEach(function(item, index) { // if there is some element which is present in intial arrat but not in current array // remove that eleemnt. //because intersection requires element to present in all arrays if (x[i].indexOf(item) === -1) { initialArray.splice(index, 1) } }) } return initialArray; } console.log(intersect(a, b, c, d)) 

There is a nice way of doing it using reduce to intersect through your array of arrays and then filter to make remaining values unique.

function intersection(arrayOfArrays) {
    return arrayOfArrays
        .reduce((acc,array,index) => { // Intersect arrays
            if (index === 0)
                return array;
            return array.filter((value) => acc.includes(value));
        }, [])
        .filter((value, index, self) => self.indexOf(value) === index) // Make values unique
    ;
}

You can iterate through each array and count the frequency of occurrence of the number in an object where the key is the number in the array and its property being the array of occurrence in an array. Using the generated object find out the lowest frequency of each number and check if its value is more than zero and add that number to the result.

 function intersection(arrayOfArrays) { const frequency = arrayOfArrays.reduce((r, a, i) => { a.forEach(v => { if(!(v in r)) r[v] = Array.from({length:arrayOfArrays.length}).fill(0); r[v][i] = r[v][i] + 1; }); return r; }, {}); return Object.keys(frequency).reduce((r,k) => { const minCount = Math.min(...frequency[k]); if(minCount) { r = r.concat(Array.from({length: minCount}).fill(+k)); } return r; }, []); } console.log(intersection([[2,3, 45, 45, 5],[4,5,45, 45, 45, 6,7], [3, 7, 5,45, 45, 45, 45,7]])) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM