简体   繁体   中英

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 }

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. 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.

 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.

 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.

 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 :

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:

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:

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

 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 } */ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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