Skip to content

Instantly share code, notes, and snippets.

@teknotica
Created February 14, 2019 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teknotica/0de052289bda2a681713923d9602ad34 to your computer and use it in GitHub Desktop.
Save teknotica/0de052289bda2a681713923d9602ad34 to your computer and use it in GitHub Desktop.
Remove duplicates from Array
const arr = [6, 8, 10, 2, 5, 6, 13, 11, 8, 0, 11, 11, 3, 5, 100, 100, 78];
Array.prototype.removeDuplicates = function () {
let arr = this;
// Sort array first
arr.sort(function(a, b) {
return a - b;
});
for (let x = 0; x < arr.length; x++) {
let pivot = arr[x];
let counter = x + 1;
if (pivot !== arr[counter]) {
continue;
} else {
do {
arr.splice(counter, 1);
} while (pivot === arr[counter]);
}
}
return arr;
};
console.time('removeDuplicates');
arr.removeDuplicates();
console.timeEnd('removeDuplicates');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment