这个问题已经讨论过了,你的答案也就是上次讨论的结果。
Array.prototype.unique = function()
{
for(var a={}, i=this.length-1; i>-1; i--)
{
if(typeof(a[this[i]])=="undefined") a[this[i]] = 1;
}
this.length=0;
for(i in a) this[this.length] = i; return this;
};
Array.prototype.unique = function()
{
for(var a={}, i=this.length-1; i>-1; i--)
{
if(typeof(a[this[i]])=="undefined") a[this[i]] = 1;
}
this.length=0;
for(i in a) this[this.length] = i; return this;
};
Array.prototype.unique2 = function()
{
for(var a={}, b={}, i=0, n=this.length; i<n; i++)
{
if(typeof(b[this[i]])!="undefined") continue;
if(typeof(a[this[i]])=="undefined") a[this[i]] = 1;
else{b[this[i]]=1; delete a[this[i]]}
}
this.length=0;
for(i in a) this[this.length] = i; return this;
}; var a = new Array(1,2,3,4);
var b = new Array(2,3,5,7);
var c = a.concat(b).unique2();
alert(c);
</script>
Array.prototype.unique2 = function()
{
for(var a={}, i=0, n=this.length; i<n; i++)
{
if(typeof(a[this[i]])=="undefined") a[this[i]] = 1;
else{ delete a[this[i]]}
}
this.length=0;
for(i in a) this[this.length] = i; return this;
}; var a = new Array(1,2,3,4);
var b = new Array(2,3,5,7);
var c = a.concat(b).unique2();
alert(c);
</script>
Array.prototype.unique2 = function()
{
for(var a={}, b={}, i=0, n=this.length; i<n; i++)
{
if(typeof(b[this[i]])!="undefined") continue;
if(typeof(a[this[i]])=="undefined") a[this[i]] = 1;
else{b[this[i]]=1; delete a[this[i]]}
}
this.length=0;
for(i in a) this[this.length] = i; return this;
}; var a = new Array(1,2,3,4);
var b = new Array(2,3,5,7);
var c = a.concat(b).unique2();
alert(c);
</script>
woodfans的代码就不行了,如果重复次数是3次,5次……就过滤不了了,比如
var a = new Array(1,2,3,3);
var b = new Array(2,2,3,4);