【问题标题】:Find indices of all duplicate records in js查找js中所有重复记录的索引
【发布时间】:2020-03-14 04:15:14
【问题描述】:

假设我有一个如下数组:

Arr1 = [12,30,30,60,11,12,30]

我需要找到在数组中重复的元素的索引,例如

ans: 0,1,2,5,6

我已经尝试过this 代码,但它只考虑使用单个元素来检查重复项。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    首先使用filter() 获取所有重复项,然后使用reduce() 获取仅在dups 中的数组元素的索引

    const arr = [12,30,30,60,11,12,30];
    const dups = arr.filter(x => arr.indexOf(x) !== arr.lastIndexOf(x));
    const res = arr.reduce((ac, a, i) => {
      if(dups.includes(a)){
        ac.push(i)
      }
      return  ac;
    }, []);
    console.log(res)

    上述算法的时间复杂度为O(n^2)。如果你想要O(n),你可以使用下面的方式

    const arr = [12,30,30,60,11,12,30];
    const dups = arr.reduce((ac, a) => (ac[a] = (ac[a] || 0) + 1, ac), {})
    const res = arr.reduce((ac, a, i) => {
      if(dups[a] !== 1){
        ac.push(i)
      }
      return  ac;
    }, []);
    console.log(res)

    【讨论】:

      【解决方案2】:

      一种稍微不同的方法,将对象作为所见项目的闭包,其中包含索引数组和第一个数组,随后是索引和值的必要展平。

      此答案基于如何将值插入到已映射值中的问题。

      这只能通过使用对象引用来实现,该引用在值出现的那一刻被保存并且以前没有看到。

      未完成结果示例

      [
          [0],
          [1],
          2,
          [],
          [],
          5,
          6
      ]
      

      最后的Array#flat 移除覆盖数组并只显示索引,如果数组保持为空,则不显示任何内容。

      [0, 1, 2, 5, 6]
      

      var array = [12, 30, 30, 60, 11, 12, 30],
          indices = array
              .map((o => (v, i) => {
                  if (o[v]) {               // if is duplicate
                      o[v][1][0] = o[v][0]; // take the first index as well
                      return i;             // return index
                  } 
                  o[v] = [i, []];           // save index
                  return o[v][1];           // return empty array
              })({}))
              .flat()                       // remove [] and move values out of array
      
      console.log(indices);

      【讨论】:

        【解决方案3】:

        你可以使用Array#reduce方法

        1. reduce循环数组。此时找到参数的索引
        2. 并使用Array#filter 检查数组中是否存在多个参数
        3. 最后将索引值推送到新的accumulator数组。如果索引值已经存在于accumulator中,则将数组的currentIndexcurInd传递给accumulator

        const arr = [12, 30, 30, 60, 11, 12, 30];
        
        let res = arr.reduce((acc, b, curInd) => {
          let ind = arr.indexOf(b);
          if (arr.filter(k => k == b).length > 1) {
            if (acc.indexOf(ind) > -1) {
              acc.push(curInd)
            } else {
              acc.push(ind);
            }
          }
          return acc;
        }, []);
        
        console.log(res)

        【讨论】:

          【解决方案4】:

          您可以使用简单的indexOf 和循环来获取重复索引。

          let arr = [12,30,30,60,11,12,30]
          let duplicate = new Set();
          for(let i = 0; i < arr.length; i++){
              let index = arr.indexOf(arr[i], i + 1);
              if(index != -1) {
                duplicate.add(i);
                duplicate.add(index);
              }
          }
          
          console.log(Array.from(duplicate).sort().toString());

          【讨论】:

            【解决方案5】:

            下面的代码将是查找重复元素索引的最简单方法

            var dupIndex = [];
            $.each(Arr1, function(index, value){
                if(Arr1.filter(a => a == value).length > 1){ dupIndex.push(index); }
            });
            

            这应该适合你

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-20
              • 1970-01-01
              • 2022-08-09
              • 2019-09-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多