Javascript常見面試題?
方法一:
代碼如下:var ary = new Array("111","22","33","111");
var s = ary.join(",")+",";
for(var i=0;i<ary.length;i++) {
if(s.replace(ary[i]+",","").indexOf(ary[i]+",")>-1) {
alert("數(shù)組中有重復(fù)元素:" + ary[i]);
break;
}
}
方法二:
代碼如下:var ary = new Array("111","22","33","111");
var nary=ary.sort();
for(var i=0;i<ary.length;i++){
if (nary[i]==nary[i+1]){
alert("數(shù)組重復(fù)內(nèi)容:"+nary[i]);
} }
方法三:
代碼如下:function isRepeat(arr){
var hash = {};
for(var i in arr) {
if(hash[arr[i]]) return true;
hash[arr[i]] = true;
}
return false;
}