【问题标题】:Remove non common items between two arrays javascript删除两个数组javascript之间的不常见项目
【发布时间】:2019-07-15 03:45:19
【问题描述】:

我有一个函数可以在 javascript 中删除两个数组之间的非公共元素,但问题是我的代码减少了数组中的一些项目并增加了一些。下面是我的代码

function canFormPairs(cleanSocks, dirtySocks) {
  let compared = [];
  cleanSocks.forEach(item => {
    dirtySocks.forEach(dItem => {
      if (item == dItem) {
        compared.push(item);
      }
    });
  });
  return compared;
}
console.log(canFormPairs([1, 5, 6, 7, 5, 6, 5, 56], [1, 5, 6, 7, 8, 5, 6, 7, 8, 78]));

上面的代码给出了

[ 1, 5, 5, 6, 6, 7, 7, 5, 5, 6, 6, 5, 5 ]

而不是期望的结果

[1, 1, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7]

排序后

请问这段代码有什么问题

【问题讨论】:

  • 你是对的,但这不是这里的问题
  • 我只是想了解是否有什么我不明白的地方,然后才能跳出来回答
  • 好吧。会编辑
  • 现在会在片场阅读。谢谢。但是这个算法有什么问题
  • 位置重要吗?左组有 2 个五,右组有 1 个 5 有关系吗?

标签: javascript arrays


【解决方案1】:

您当前的逻辑为两个数组之间的每个唯一索引匹配推送项目。例如,对于 7,7 在索引 3(第一个数组)和索引 3(第二个数组)处匹配,因此它被推送一次。然后,下一个匹配是索引 3(第一个数组)和索引 7(第二个数组)。除了3-33-7 之外,没有更多的index 匹配项,因此只有两个7(值)被推送。

我会考虑从两个数组中创建一个Set,然后合并两个数组并使用.filter 删除不在两个集合中的元素,然后对数组进行排序:

function canFormPairs(a, b) {
  const setA = new Set(a);
  const setB = new Set(b);
  return [...a, ...b]
    .filter(item => setA.has(item) && setB.has(item))
    .sort((a, b) => a - b);
}
console.log(canFormPairs([1, 5, 6, 7, 5, 6, 5, 56], [1, 5, 6, 7, 8, 5, 6, 7, 8, 78]));

【讨论】:

    【解决方案2】:

    试试这个

    function canFormPairs(cleanSocks, dirtySocks) {
      let compared = [];
      let one = cleanSocks.filter(el => dirtySocks.includes(el));
      let two = dirtySocks.filter(el => cleanSocks.includes(el));
      compared = one.concat(two);
      compared.sort(function(a, b) {
        return a - b;
      });
      return compared;
    }
    console.log(canFormPairs([1, 5, 6, 7, 5, 6, 5, 56], [1, 5, 6, 7, 8, 5, 6, 7, 8, 78]));

    【讨论】:

      【解决方案3】:

      让这个答案专注于为什么 OP 的代码会给出不正确的结果

      你的算法看起来像,

      • 有 2 个数组,ab
      • 循环a
      • 循环b
      • 如果a[i] === b[i],将a[i] 推送到结果。

      问题:

      • 结果数组中有m*n 项。所以对于1,因为两者都只有一次,所以你只有一次。同样对于5,它的2*3 因此你得到6 次而不是5。输出应为m+n instead
      • 这两个数组都可以有重复且未排序。所以最终结果不会被排序。

      【讨论】:

        【解决方案4】:

        我对您的功能进行了一些更改,现在可以使用:

        function canFormPairs(cleanSocks, dirtySocks) {
            let compared = [];
            let cleanSocksUniqueItems = [];
            cleanSocks.map((item, index) => {
                cleanSocks.slice(index+1).map(elem => {
                    if (item === elem && !cleanSocks.slice(0, index).includes(item)) {
                       dirtySocks = [...dirtySocks, elem];
                    }
                });
            });
            cleanSocksUniqueItems = Array.from(new Set(cleanSocks));
            cleanSocksUniqueItems.forEach(item => {
                dirtySocks.forEach(dItem => {
                    if (item === dItem) {
                        if (!compared.includes(item)) {
                            compared = [...compared, item];
                        }
                        compared = [...compared, item];
                    }
                });
           });
           return compared.sort();
        }
        console.log(canFormPairs([1, 5, 6, 7, 5, 6, 5, 56], [1, 5, 6, 7, 8, 5, 6, 7, 8, 78]));
        

        【讨论】:

          【解决方案5】:

          这是一个使用flatMap的简单解决方案,看看。

          Array.prototype.pairs = function(arr2){
          var arr1= this;
          let compared = [arr1, arr2].flatMap(function (x)
          {
            return x.filter(a=> arr1.indexOf(a) != -1 && arr2.indexOf(a) != -1)
          }).sort((a,b)=> a-b);
          
          return compared
          }
          
          var res = [1, 5, 6, 7, 5, 6, 5, 56].pairs([1, 5, 6, 7, 8, 5, 6, 7, 8, 78])
          console.log(res);

          【讨论】:

            猜你喜欢
            • 2019-05-24
            • 1970-01-01
            • 2017-06-01
            • 2017-10-08
            • 1970-01-01
            • 2022-12-02
            • 2020-08-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多