Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Created December 13, 2019 21:28
Show Gist options
  • Save jiggzson/694612c82223f1518ee497226988e471 to your computer and use it in GitHub Desktop.
Save jiggzson/694612c82223f1518ee497226988e471 to your computer and use it in GitHub Desktop.
This function removes duplicates from an array. It can also be used to remove duplicates that are near another value with a tolerance
var removeDuplicates = function(arr, condition) {
var conditionType = typeof condition;
if(conditionType !== 'function' || conditionType === 'undefined') {
condition = function(a, b) {
return a === b;
};
}
var seen = [];
while(arr.length) {
var a = arr[0];
//only one element left so we're done
if(arr.length === 1) {
seen.push(a);
break;
}
var temp = [];
seen.push(a); //we already scanned these
for(var i=1; i<arr.length; i++) {
var b = arr[i];
//if the number is outside the specified tolerance
if(!condition(a, b))
temp.push(b);
}
//start over with the remainder
arr = temp;
}
return seen;
};
// ===============================================================================================
// USAGE:
var arr = ['matt', 'tom', 'tina', 'matt'];
console.log(removeDuplicates(arr));
//Remove duplicates within a tolerance. The first value seen will be returned.
var arr2 = [0, 3.0001, 1.000001, 2, 2.1, 3, 1, 2.99999999999, 4, 2, 0, 3];
console.log(removeDuplicates(arr2, (a, b) => Math.abs(a-b) <= 0.001));
var isInt = function(n) {
return n % 1 === 0;
};
//Move the integers to the front if you want them to be seen first
arr2 = arr2.sort(function(a, b) {
var ret = 0;
if( isInt(a) && isInt(b)) {
ret = 3;
}
else if(isInt(a)) {
ret = 2;
}
else if(isInt(b)) {
ret = 1;
}
return ret;
});
console.log(removeDuplicates(arr2, (a, b) => Math.abs(a-b) <= 0.001));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment