简体   繁体   English

如何在一个任务中收集两个 arrays 的交集和补码?

[英]How to collect the intersection and both complements of two arrays within one task?

Arrays - Arrays -

const source = [1, 1, 1, 3, 4];
const target = [1, 2, 4, 5, 6];

Empty arrays -空 arrays -

const matches1 = [];
const matches2 = [];
const unmatches1 = [];

Loop -环形 -

for (const line of source) {
  const line2Match = target.find((line2) => {
    const isMatch = line === line2;
    return isMatch;
  });

  if (line2Match) {
    matches1.push(
      line
    );
    matches2.push(line2Match);
  } else {
    unmatches1.push(line);
  }
}

This is the output right now -这是现在的 output -

[ 1, 1, 1, 4 ] at matches1 [ 1, 1, 1, 4 ]在比赛1

[ 3 ] at unmatches1 [ 3 ]在无与伦比1

[ 1, 1, 1, 4 ] at matches2 [ 1, 1, 1, 4 ]在比赛2

The desired output -所需的 output -

[ 1, 4 ] at matches1 [ 1, 4 ]在比赛中1

[ 1, 1, 3 ] at unmatches1 [ 1, 1, 3 ]不匹配1

[ 1, 4 ] at matches2 [ 1, 4 ]在比赛中2

What I would like to add is when source has a match with target , the value will be deleted from the target array, how can I achive that?我想补充的是,当sourcetarget匹配时,该值将从target数组中删除,我该如何实现?

The OP... OP...

"What I would like to add is when source has a match with target, the value will be deleted from the target array, how can I achive that?" “我想补充的是,当源与目标匹配时,该值将从目标数组中删除,我该如何实现?”

One had to actively mutate the target array by slicing the common item from it which in the very end makes target become the relative complement of source in target ... commonly/vulgo... the target difference .必须通过从中切出公共项目来主动改变target数组,这最终使target成为targetsource的相对补充... common/vulgo...目标差异

One of cause could apply a shallow copy of target as part of the initial value of a reduce task in order to not mutate the original target reference itself...一个原因可能是应用target的浅表副本作为reduce任务的初始值的一部分,以便不改变原始target引用本身......

 function collectIntersectionAndComplements(collector, sourceItem) { const { targetDiff } = collector; const targetIndex = targetDiff.findIndex(targetItem => targetItem === sourceItem); if (targetIndex === -1) { // collect the relative complement of target // in source... vulgo... the source difference. (collector.sourceDiff??= []).push(sourceItem) } else { // collect the intersection of both source and target... (collector.intersection??= []).push( //... by rejecting the common item from the original // target, thus actively mutating the latter, which leads // to ending up additionally with the relative complement // of source in target... vulgo... the target difference. targetDiff.splice(targetIndex, 1)[0] ); } return collector; } const source = [1, 1, 1, 3, 4]; const target = [1, 2, 4, 5, 6]; const { intersection, sourceDiff, targetDiff, } = source.reduce(collectIntersectionAndComplements, { targetDiff: [...target] }); console.log({ source, target, intersection, sourceDiff, targetDiff });
 .as-console-wrapper { min-height: 100%;important: top; 0; }

@heyheyhey2 ... Btw, if one was talking about the Simple Theory of Sets then duplicate (identical) values within each array/list/set were not allowed. @heyheyhey2 ...顺便说一句,如果有人在谈论集合的简单理论,那么每个数组/列表/集合中的重复(相同)值是不允许的。 Sets always have to feature just unique values.集合总是必须具有唯一值。

Just for matches1: (similar for matches2)仅适用于matches1:(类似matches2)

const source = [1, 1, 1, 3, 4];
const target = [1, 2, 4, 5, 6];

let matches1 = source.reduce((res, curr)  => {
    if (target.includes(curr) && !res.includes(curr)) {
        res.push(curr);
    }
    return res;
}, []
);

console.log(matches1)
//[1,4]

For unmatches1:对于不匹配1:

let unmatches1 = source.reduce((res, curr)  => {
    if ((target.includes(curr) && res[0].includes(curr)) || !target.includes(curr)) {    
        res[1].push(curr);
    }
    if (target.includes(curr)) {
        res[0].push(curr)
    }
    return res;
}, [[],[]]
)[1]

console.log(unmatches1)
//[1,1,3]

Note that for a single pass both arrays must have been sorted into ascending order.请注意,对于单次通过,arrays 必须已按升序排序。

function go(){
    const a = [0,1, 1, 1, 3, 4, 6,8,10];
    const b = [-3,-4,1, 2, 4, 5, 6,25,26];

    let nomatcha=[];
    let match=[];
    let nomatchb=[];
    let i=0,j=0;
    while(true){
        if(i>=a.length){
            for (;j<b.length;j++) nomatchb.push(b[j]);
            break;
        }
        else if (j>=b.length){
            for (;i<a.length;i++) nomatcha.push(a[j]);
            break;
        }
        else if(a[i]==b[j]){
            match.push(a[i++]);
            j++;
        }
        else if (a[i]<b[j]){
            let val=a[i++];
            if(nomatcha.length==0)nomatcha.push(val);
            else if(val != nomatcha[nomatcha.length-1])
                nomatcha.push(val);
        }
        else if (b[j]<a[i]){
            let val=b[j++];
            if(nomatchb.length==0)nomatchb.push(val);
            else if(val != nomatchb[nomatchb.length-1])
                nomatchb.push(val);
        }


    }

    console.log("match: "+match);
    console.log("nomatcha: "+nomatcha);
    console.log("nomatchb: "+nomatchb);

}

} }

Output: Output:

match: 1,4,6 nomatcha: 0,1,3,8,10 nomatchb: -3,-4,2,5,25,26匹配:1,4,6 nomatcha:0,1,3,8,10 nomatchb:-3,-4,2,5,25,26

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

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