繁体   English   中英

如何检查二维数组的重复项

[英]how to check 2D array for duplicates

我试图从我的二维数组中删除重复项,但没有成功。

这是我正在做的任何想法在哪里犯错?

 function Remove_duplicates_from_2d_array(data_array) { if (data_array.length > 0) { let unique_index_counter = 0; // loop on target array for (var a = 0; a < data_array.length; a++) { var unique_array = data_array[unique_index_counter]; if (a === unique_index_counter) { continue; } console.log('comparing index: ' + a + ' ( ' + data_array[a] + ' ) -- index: ' + a + ' ( ' + data_array[a] + ' )'); if (data_array[a].sort().join(',') === unique_array.sort().join(',')) { console.log('match it index ' + a + ' - ' + unique_index_counter); // same arrays data_array.splice(a, 1); a = 0; // reset for loop as splice will rebuilt array } // a will be equal to data_array length incase there is no match found if (a === data_array.length) { unique_index_counter++; } if(unique_index_counter != data_array.length) { a = 0; // reset for loop because we have not checked all items } } return data_array; } else { return []; } } var a1 = [1, 2, 3]; b1 = [4, 4, 5]; c1 = [3, 4, 5]; d1 = [4, 4, 5]; var data_array = []; data_array.push(a1); data_array.push(b1); data_array.push(c1); data_array.push(d1); console.log('original array.'); console.log(data_array); var r = Remove_duplicates_from_2d_array(data_array); console.log('unique array.'); console.log(r); // [[1,2,3],[4,4,5],[3,4,5]]

您可以为看到的排序字符串Set一个Set并通过检查出现来过滤数组。

 function removeDuplicates(array) { var seen = new Set; return array.filter(a => (s => !seen.has(s) && seen.add(s))(a.join())); } console.log(removeDuplicates([[1, 2, 3], [4, 4, 5], [3, 4, 5], [4, 4, 5]])); // [[1, 2, 3], [4, 4, 5], [3, 4, 5]]
 .as-console-wrapper { max-height: 100% !important; top: 0; }

带着对象。

 function removeDuplicates(array) { var seen = {}; return array.filter(v => !seen[v] && (seen[v] = true)); } console.log(removeDuplicates([[1, 2, 3], [4, 4, 5], [3, 4, 5], [4, 4, 5]])); // [[1, 2, 3], [4, 4, 5], [3, 4, 5]]
 .as-console-wrapper { max-height: 100% !important; top: 0; }

你可以Array.prototype.reduce()源数组做Array.prototype.find() 'ing 沿途重复:

 const src = [[1,2,3],[4,4,5],[3,4,5],[4,4,5]], arrsAreEqual = (a1,a2) => a1.sort().join('|') === a2.sort().join('|'), dedupe = src.reduce((r,a) => (!r.find(ra => arrsAreEqual(a,ra)) && r.push(a), r), []) console.log(JSON.stringify(dedupe))
 .as-console-wrapper{min-height:100%;}

Set很好,但您也可以从基于for的简单方法中受益:

 var orig = [[1, 2, 3], [4, 4, 5], [3, 4, 5], [4, 4, 5]]; function undupe(arr){ let ret=[]; for(let candidate of arr){ let found=false; for(let line of ret){ let linematch=true; for(let i=0;i<line.length;i++){ if(candidate[i]!==line[i]){ linematch=false; // if one element does not match, the lines are different break; // and the rest does not matter } } if(linematch){ found=true; // if all elements matched, candidate is a duplicate break; // remaining lines do not matter } } if(!found){ ret.push(candidate); // candidate is unique, add it to the result } } return ret; } console.log(undupe(orig));

  • candidate循环获取输入数组中的所有项目
  • line循环从ret数组中获取所有项目,因此到目前为止唯一的项目
  • i循环比较实际数字(来自candidate数字与来自line的相应数字)
  • 这些循环在大数据时很慢,所以尽快终止它们是个好主意。 就像当一对行中的第一个元素不匹配时,它们肯定是不同的,没有必要检查其余的。

当然,它可以更结构化,比如

 var orig = [[1, 2, 3], [4, 4, 5], [3, 4, 5], [4, 4, 5]]; function linesmatch(line1,line2){ for(let i=0;i<line1.length;i++) if(line1[i]!==line2[i]) return false; return true; } function isdupe(arr,line){ for(let l of arr) if(linesmatch(l,line)) return true; return false; } function undupe(arr){ let ret=[]; for(let candidate of arr) if(!isdupe(ret,candidate)) ret.push(candidate); return ret; } console.log(undupe(orig));

这将是更像教科书般的变体,具有相互叠加的小型可读功能。

这是我基于 for 循环的重复去除器的固定简单版本

function Remove_duplicates_from_2d_array(data_array) {
    var unique_array = [];
  if (data_array.length > 0) {
    let unique_index_counter = 0; 
    // loop on target array
    for (var a = 0; a < data_array.length; a++) {
      // if have same indexs , skip
      if (a === unique_index_counter) {
         continue;
      }
        // compare both indexes
      if (data_array[unique_index_counter].join(',') == data_array[a].join(',') ) {
            data_array.splice(a, 1);  // remove that a index 
            a = 0; // reset for loop as splice will rebuilt array 
            continue;
      }
      // a will be equal to data_array length incase there is no match found 
      else if ( (data_array.length != 0 && a == data_array.length - 1)  ) {
        unique_array.push(data_array[unique_index_counter]);  // push unique index to unique array
        data_array.splice(unique_index_counter, 1);  // remove that a index 
        a = 0; // reset for loop as splice will rebuilt array 
      }
    } // for end
     // by now one unique element will be still left in source arrays
     unique_array.push(data_array[0]);  // push unique index to unique array

    return unique_array;
  } else {
    return [];
  }
}

var a1 = [1, 2, 3];
b1 = [4, 4, 5];
c1 = [3, 4, 5];
d1 = [4, 4, 5];
var data_array = [];
data_array.push(a1);
data_array.push(b1);
data_array.push(c1);
data_array.push(d1);
console.log('original array.');
console.log(data_array);

var r = Remove_duplicates_from_2d_array(data_array);
console.log('unique array.');
console.log(r);  // [[1,2,3],[4,4,5],[3,4,5]]

暂无
暂无

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

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