Skip to content

Instantly share code, notes, and snippets.

@gracefullight
Last active December 1, 2017 01:34
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 gracefullight/de964e6a026279a424e67f4eb5524b2f to your computer and use it in GitHub Desktop.
Save gracefullight/de964e6a026279a424e67f4eb5524b2f to your computer and use it in GitHub Desktop.
/*
@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