為array添加了一個method:unique(),採用關聯結構的方式,效率很高。
var array1=new Array("a","c","b","b","a","c","c","c");
Array.prototype.unique = array_unique;
function array_unique(){
var o = new Object();
for (var i=0,j=0; iif (typeof o[this[i]] == 'undefined'){
o[this[i]] = j++;
}
}
this.length = 0;
for (var key in o) {
this[o[key]] = key;
}
return this;
}
var d = new Date().getTime();
document.write(array1.unique());
d = new Date().getTime()-d;
document.write("
2000节点 新算法计耗时 "+ d +" 毫秒!");