简体   繁体   English

删除有序数组中的重复项

[英]Removing duplicates in an ordered array

What I'm trying to do is find how many times an array elements repeats itself in array, push the element along with the number of repeats it has in an object and after that delete the element and all its duplicates. 我要尝试做的是发现数组元素在数组中重复自身的次数,将元素及其在对象中的重复次数一起推送,然后删除该元素及其所有重复项。

At the moment I have this function : 目前,我有这个功能:

function getDuplicates(arr) {
  let lastIndex = null;
  let obj = {};
  for ( let i = 0; i < arr.length; i++ ) {
    lastIndex = arr.lastIndexOf(arr[i]);
    obj[arr[i]] = lastIndex + 1;
    arr.splice(0, lastIndex + 1 );
  }
  console.log(obj);
}

getDuplicates([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ]);

which logs : { '1': 4, '2': 2, '3': 4, '5': 5 } 该日志: { '1': 4, '2': 2, '3': 4, '5': 5 }

It works great for the first 3 numbers ( 1,2 and 3 ) but 4 doesnt show up, 5 is messed up and 6 doesnt show due to lastIndex +1. 它对于前3个数字(1,2和3)非常有效,但是由于lastIndex +1,没有出现4个,弄乱了5个,没有显示6个。 Am I missing something or is there a better way to do this ? 我是否缺少某些东西,或者有更好的方法吗?

Thank you. 谢谢。

Here's one method how to solve it. 这是解决问题的一种方法。

Firstly I've removed all duplicated elements from the given array, using new Set() and then iterated over it using Array#forEach and checked with Array#filter how many times given element appears in the passed array. 首先,我使用new Set()从给定数组中删除了所有重复的元素,然后使用Array#forEach在其上进行迭代,并使用Array#filter检查了给定元素在传递的数组中出现了多少次。

 function getDuplicates(arr){ var filtered = [...new Set(arr)], result = {}; filtered.forEach(function(v){ result[v] = arr.filter(c => c == v).length; }) console.log(result); } getDuplicates([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ]); 

Array#reduce solution. Array#reduce解决方案。

 function getDuplicates(arr) { var res = arr.reduce(function(s, a) { s[a] = arr.filter(c => c == a).length; return s; }, {}); console.log(res); } getDuplicates([1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6]); 

You can simplify a lot the logic. 您可以简化很多逻辑。 Just an object to count and an if statement to increment values or define as 1 if it wasn't defined. 只是一个要计数的对象,一个if语句用于增加值或如果未定义则定义为1。

 function countDuplicates(arr) { // Contains a pair of values an instances. var counting = {}; // Iterate array: check if already counted. If yes, increment, if not define as 1. for (el of arr) (counting[el]) ? counting[el]++ : counting[el] = 1; console.log(counting); return counting; } countDuplicates([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ]); 

Adding, if you also want to get the unique elements, you can just use E6 set : 另外,如果您还想获得唯一元素,则可以使用E6 set

var set = new Set([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ]);

It looks as if you want to COUNT duplicates, but if all you want to do is remove duplicates (As headline states), as per @ChantryCargill s suggestion: 看起来好像您想计数重复项,但是如果您要做的只是按照@ChantryCargill的建议删除重复项(如标题所示):

function removeDuplicates (arr) {
   var results = [];
   for(var i = 0; i < arr.length; i++) {
      var item = arr[i];
      if(results.indexOf(item) === -1) {
         results.push(item);
      }
   }
   return results;
}

console.log(removeDuplicates([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ])); 
//[1, 2, 3, 4, 5, 6]

If you want to COUNT duplicates: 如果您要COUNT个重复项:

function getDuplicates(arr) {
   var results = {};
   for(var item of arr) {
      if(!results[item]) {
         results[item] = 0;
      }
      results[item]++;
   }
   return results;
}

console.log(getDuplicates([ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6 ])); 
//{"1":4,"2":2,"3":4,"4":2,"5":3,"6":1}

You can simply use Array#reduce() to count the occurrences and Array#filter() to remove the duplicates 您可以简单地使用Array#reduce()来计数出现次数,并使用Array#filter()来删除重复项

 getDuplicates([1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 6]); function getDuplicates(arr) { var obj = arr.reduce((map, item) => (map[item] = ++map[item] || 1, map),{} ); var withoutDup = arr.filter((item, pos) => arr.indexOf(item) == pos); console.log(JSON.stringify(obj)); console.log(JSON.stringify(withoutDup)); } 

You can count and print as you would want like this: 您可以根据需要进行计数和打印:

function getDuplicates(arr) {
    var counts = {};
    arr.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
    console.log(counts);
}
function getDuplicates(arr) {
  let lastNum = null;
  let obj = {};
  for ( let i = 0; i < arr.length; i++ ) {
    if (arr[i] != lastNum){
      lastNum = arr[i];
      obj[arr[i]] = 1;
    }else{
      obj[arr[i]]++;
    }
  }
  console.log(obj);
}

Try this: 尝试这个:

 function getDuplicates(){ var numbers=Array.prototype.slice.call(arguments); var duplicates={}; for(var index in numbers){ if(numbers.indexOf(numbers[index])==index) continue; duplicates[numbers[index]]= (duplicates[numbers[index]] || 0) + 1; } return duplicates; } console.log(getDuplicates(1,2,3,1,1,3,4,5,6,7,8,6)); /* prints { 1: 2, 3: 1, 6: 1 } */ 

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

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