js怎么将一个二维数组中相同元素提取出来?

例如:var a=[[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];
输出:[2,3]
例如:var a=[[0,1,2,3,5],[-1,1,4,5,6,7],[1,2,3,5,6]];
输出:[1,5]

阅读 712
评论
    11 个回答

    a.reduce((pre, cur) => {

        let newArr = [];
        cur.forEach((v, i) => {
            if (pre.indexOf(v) >= 0) {
                newArr.push(v);
            }
        })
        pre = newArr;
        return pre;
    })

      一行代码的事。。。

          a[0].filter(item => a.every(arr =>arr.includes(item)))
        • 1.7k

        方法肯定是很多,这里提供一种思路。

        var a=[[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];
        
        function getRepeatValues(arr) {
            var obj = arr.reduce((res,item) => {
                new Set(item).forEach(v => {
                    if(res[v]) res[v]++;
                    else res[v] = 1
                })
                return res;
            }, {});
            return Object.keys(obj).filter(k => obj[k]==arr.length).map(Number);
        }
        
        getRepeatValues(a)
          var a=[[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];
          
          function getRepeatValues(arr){
            const original = arr.flat()
            const uniqueItems = new Set();
            const duplicates = new Set();
            for (const value of original){
              if (uniqueItems.has(value)) {
                duplicates.add(value);
                uniqueItems.delete(value);
              } else {
                uniqueItems.add(value);
              }
            }
            return [...duplicates]
          }
          getRepeatValues(a)

          参考:
          https://codereview.stackexcha...

            • 1.6k

            我有一个比较取(qi)巧(pa)的思路.

            1. 首先把这个数组a每个成员都去重;
            2. 再把去重后的数组a降维成1维数组;
            3. 统计这个1维数组里每个元素出现的次数,出现次数等于a的length的元素就是结果
            var a = [[1, 2, 3, 5], [2, 3, 4, 4, 4, 4], [2, 3], [2, 3, 6, 7]];
            
            const uniq = arr => [...new Set(arr)];
            
            const flat = arr => arr.reduce((p, i) => [...p, ...i], []);
            
            const count = arr => arr.reduce((p, i) => ({...p, [i]: p[i] ? p[i] + 1 : 1}), {});
            
            const filter = (obj, length) => Object.keys(obj).filter(k => obj[k] === length);
            
            console.log(filter(count(flat(a.map(uniq))), a.length));
              • 3
              • 新人请关照

              优化算法之一:
              先对二维数组中的元素(一维数组),按数组长度排序,先对长度最小的前两个元素(一维数组),求交集(for each遍历第一个数组的元素,检查是否在第2个数组出现)
              然后,用这个结果与后面的元素,进行迭代求交集。

                • 580
                function getTheSame(arr) {
                  return arr.reduce(function(a, b) {
                    return a.filter(function(item) {
                      return b.includes(item);
                    });
                  });
                }
                  const similar = function(arrs) {
                        var arr = arrs.shift(); 
                        for(var i=arrs.length;i--;){
                           var p = {"boolean":{}, "number":{}, "string":{}}, obj = [];
                           arr = arr.concat(arrs[i]).filter(function (x) {
                             var t = typeof x;
                             return !((t in p) ? !p[t][x] && (p[t][x] = 1) : obj.indexOf(x) < 0 && obj.push(x));
                           });
                           if(!arr.length) return null; 
                        }
                        return arr;
                  }

                  亲测有效

                    无非降维,去重,计数,求重

                    var a = [[1, 2, 3, 5], [2, 3, 4], [2, 3], [2, 3, 6, 7]];
                    
                    // 1.降维
                    var arr1 = [].concat(...a)
                    console.log(arr1)
                    
                    // 2.去重
                    var uniq = [...new Set(arr1)]
                    console.log(uniq)
                    
                    // 3. 计数
                    var counter = arr1.reduce((acc, v) => {
                        if (v in acc) {
                            acc[v]++
                        } else {
                            acc[v] = 1
                        }
                        // 4.非重
                        // if(acc[v] > 1 && uniq.indexOf(v) > -1){
                        //     uniq.splice(uniq.indexOf(v),1)
                        // } 
                        return acc
                    }, {})
                    console.log(counter)
                    
                    // 4.求重
                    var duplicate = uniq.filter(e => counter[e] > 1)
                    console.log(duplicate)
                    

                      const list = [[1,2,3,5], [2,3,4], [2,3], [2,3,6,7]];
                      const rlt = list.reduce((sameV, item) => {

                      const newSame = [];
                      if (sameV && sameV.length > 0) {
                          item.filter((inner) => {
                              if (sameV.indexOf(inner) > -1) {
                                  newSame.push(inner);
                              }
                          });
                      }
                      return newSame;

                      });
                      console.log(rlt);

                        • 439

                        时间复杂度为 O(n)

                        var a = [
                          [1, 2, 3, 5],
                          [2, 3, 4],
                          [2, 3],
                          [2, 3, 6, 7]
                        ]
                        let obj = {}
                        let repeatAry = []
                        
                        function mapAry(ary) {
                          ary.forEach(item => {
                            if (Array.isArray(item)) {
                              mapAry(item)
                            } else {
                              obj[item] = (obj[item] || 0) + 1
                              obj[item] === 2 && repeatAry.push(item)
                            }
                          })
                        }
                        mapAry(a)
                        console.log(repeatAry)
                          撰写回答

                          登录后参与交流、获取后续更新提醒