Answer the question
In order to leave comments, you need to log in
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
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;
}
const uniq = (arr) => {
let obj = {};
arr.forEach(elem=>{
obj[elem] = null;
});
return Object.keys(obj);
};
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]);
}
}
arr = arr.filter(function (item, pos) {
return arr.indexOf(item) == pos;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question