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
/* | |
* I saw this thread: http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array | |
* The solutions from gist:1305056 were elegant, but wrong. So here's mine: | |
*/ | |
Array.prototype.unique = function(test) { | |
/* returns a new, sorted Array without duplicates */ | |
if (!Array.isArray(this)) | |
throw new TypeError("Array.prototype.unique must be called on an Array"); | |
return this.slice(0).sort().filter( typeof test == "function" | |
? function(v, i, a) { return !i || !test(v, a[i-1]); } |