Last active
December 1, 2017 01:34
-
-
Save gracefullight/de964e6a026279a424e67f4eb5524b2f to your computer and use it in GitHub Desktop.
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
/* | |
@author http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/ | |
*/ | |
// prototype | |
Array.prototype.unique = function() { | |
var o = {}, i, r = []; | |
var l = this.length; | |
for (i = 0; i < l; i++) { | |
o[this[i]] = this[i]; | |
} | |
for (i in o) { | |
r.push(o[i]); | |
} | |
return r; | |
}; | |
// function | |
var uniqueArray = function(arr){ | |
var o = {}, i, r=[]; | |
var l = arr.length; | |
for (i = 0; i < l; i++) { | |
o[arr[i]] = arr[i]; | |
} | |
for (i in o) { | |
r.push(o[i]); | |
} | |
return r; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment