繁体   English   中英

在数组中查找数组中的交集元素

[英]find intersection elements in arrays in an array

我需要构造一个函数交集,该交集比较输入数组,并返回一个包含所有输入中的元素的新数组。 如果每个数组中的数字仅重复一次,则以下解决方案有效,否则将中断。 另外,我不知道如何简化并且不使用凌乱的循环:

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;
}

试试:

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]

希望这个片段会有用

 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)) 

有一种很好的方法,使用reduce可以使数组的数组相交,然后进行filter以使剩余值唯一。

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
    ;
}

您可以遍历每个数组,并计算对象中数字出现的频率,其中键是数组中的数字,其属性是数组中出现的数组。 使用生成的对象找出每个数字的最低频率,并检查其值是否大于零并将该数字添加到结果中。

 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]])) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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