Array.prototype.unique = function(){ var elem, i=1, dup=[], j=0; this.sort(); for( ; (elem=this[i]); i++){ if(elem===this[i-1]) j=dup.push(i);//目标中重复元素的索引压入dup中。 } while(j--){ this.splice(dup[j], 1);//去目标数组中去掉重复元素 } }
Array.prototype.unique = function(){ var elem, i=1, dup=[], j=0; this.sort(); for( ; (elem=this[i]); i++){ if(elem===this[i-1]) j=dup.push(i);//目标中重复元素的索引压入dup中。 } while(j--){ this.splice(dup[j], 1);//去目标数组中去掉重复元素 } }