// prototype 방식
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
// function 방식
var uniqueArray = function(arr){
var o = {}, i, l = arr.length, r = [];
for(i=0; i<l;i+=1) o[arr[i]] = arr[i];
for(i in o) r.push(o[i]);
return r;
};
// 예
var testArray = [1,2,1,3];
testArray = testArray.unique();
testArray = uniqueArray(testArray);
console.log(testArray); // [1,2,3];
[출처] Javascript Unique Array (가장 빠른 알고리즘)|작성자 Danny
|