A
A
Alexander Knyazev2017-01-31 09:42:55
JavaScript
Alexander Knyazev, 2017-01-31 09:42:55

What is the fastest way to remove duplicates from an array?

I have an array with approximately 300,000 elements (phone numbers). I need the same element not to appear twice in this array. How to process such an array without waiting for the end of the world?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Belyaev, 2017-01-31
@alexandrknyazev13071995

The fastest way is to wrap the array in a Set and get a new array from it: arr = Array.from(new Set(arr));But it only works with primitives (strings, numbers, bools) because objects are compared by reference, the output will be a new array
If you need to perform manipulations on the current array, then there is a slightly more complicated way:

function dedupe(arr) {
  const values = new Set();
  const len = arr.length;
  let offset = 0;
  for(let i = 0; i < len; i++) {
    let val = arr[i];
    if(values.has(val)) {
      offset++;
    } else {
      values.add(val);
      arr[i - offset] = val;
    }
  }
  arr.length = len - offset;
}

V
Vitaly, 2017-01-31
@vshvydky

const uniq = (arr) => {
    let obj = {};    
    arr.forEach(elem=>{
        obj[elem] = null;
    });
    return Object.keys(obj);
};

V
Viktor Volkov, 2018-03-17
@VGVolkov

I sorted the array, then ran a loop with the condition that only the element in front of which is not with the same value is added to the resulting array. There were no failures in the tests.
varres = [];
newarr.sort();
for(let i = 0; i < newarr.length; i++){
if(newarr[i] != newarr[i+1]){
res.push(newarr[i]);
}
}

K
Kirill Nefediev, 2019-08-07
@Traineratwot

arr = arr.filter(function (item, pos) {
                return arr.indexOf(item) == pos;
            });

the fastest and most reliable (of those that I came across)
Speed ​​TESTS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question