判断数组是否有重复值的三种方法
function isRepeat1(arr){
var s = arr.join(",")+",";
for(var i=0;i
if(s.replace(arr[i]+",","").indexOf(arr[i]+",")>-1) {
alert("数组中有重复元素:" + arr[i]);
break;
}
}
}
function isRepeat2(arr){
var nary=arr.sort();
for(var i=0;i
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i]);
}
}
}
function isRepeat3(arr){
var hash = {};
for(var i in arr) {
if(hash[arr[i]])
return true;
hash[arr[i]] = true;
}
return false;
}
ar ary = new Array("111","22","33","111");
var s = ary.join(",")+",";
for(var i=0;i
if(s.replace(ary[i]+",","").indexOf(ary[i]+",")>-1) {
alert("数组中有重复元素:" + ary[i]);
break;外语屋
}