Created
February 14, 2019 09:35
-
-
Save teknotica/0de052289bda2a681713923d9602ad34 to your computer and use it in GitHub Desktop.
Remove duplicates from Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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