Array.prototype.distinct=function(){
var ret=[];
for(i=0,ii=this.length;i<1*ii;i++)
{
for(j=i+1;j<1*this.length;)
{
if(this[i]===this[j])
{
ret.push(this.splice(j,1)[0]);
}else{
j++;
}
}
}
this.ret=ret;
return this;
}
//测试
alert(['a','b','c','c','d','b','a','a','b','d'].distinct());
//alert(['a','b','c','c','d','b','a','a','b','d'].distinct().ret);
var ret=[];
for(i=0,ii=this.length;i<1*ii;i++)
{
for(j=i+1;j<1*this.length;)
{
if(this[i]===this[j])
{
ret.push(this.splice(j,1)[0]);
}else{
j++;
}
}
}
this.ret=ret;
return this;
}
//测试
alert(['a','b','c','c','d','b','a','a','b','d'].distinct());
//alert(['a','b','c','c','d','b','a','a','b','d'].distinct().ret);
2
Array.prototype.makeUnique=function()
{
var a = [],
b = {},
ret = [];
for (var i=0,l=this.length;i < 1*l;i++)
{
if(!(this[i] in b))
{
a.push(this[i]);
b[this[i]]=this[i];
}
else
{
ret.push(this[i]);
}
}
a.ret = ret;
return a;
}
//测试
alert(['a','b','c','c','d','b','a','a','b','d'].makeUnique());
alert(['a','b','c','c','d','b','a','a','b','d'].makeUnique().ret);
{
var a = [],
b = {},
ret = [];
for (var i=0,l=this.length;i < 1*l;i++)
{
if(!(this[i] in b))
{
a.push(this[i]);
b[this[i]]=this[i];
}
else
{
ret.push(this[i]);
}
}
a.ret = ret;
return a;
}
//测试
alert(['a','b','c','c','d','b','a','a','b','d'].makeUnique());
alert(['a','b','c','c','d','b','a','a','b','d'].makeUnique().ret);
3
function unique(oArray) {
var a = {}, b = [];
oArray instanceof Array ? function(o) {
for(var i = 0;i < o.length;i++) {
a[o[i]] = o[i]
}
for(var i in a) {
b.push(a[i])
}
}(oArray) : b.push("\u4f60\u4e2b\u4f20\u7684\u662f\u4ec0\u4e48\u73a9\u610f\uff1f");
return b
};
var a = {}, b = [];
oArray instanceof Array ? function(o) {
for(var i = 0;i < o.length;i++) {
a[o[i]] = o[i]
}
for(var i in a) {
b.push(a[i])
}
}(oArray) : b.push("\u4f60\u4e2b\u4f20\u7684\u662f\u4ec0\u4e48\u73a9\u610f\uff1f");
return b
};