Home >Web Front-end >JS Tutorial >Three practical methods for JS to determine whether there are duplicate values ​​in an array_javascript skills

Three practical methods for JS to determine whether there are duplicate values ​​in an array_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:25:261447browse

Method 1:

Copy code The code is as follows:

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("There are duplicate elements in the array: " ary[i]);

break;Foreign Language House

}

}

Method 2:
Copy code The code is as follows:

var ary = new Array(" 111","22","33","111");

var nary=ary.sort();

for(var i=0;i
if (nary[i]==nary[i 1]){

alert("Array duplicate content: " nary[i]);

}

}

Method three: Inland transportation
Copy code Code As follows:

function isRepeat(arr){

var hash = {};

for(var i in arr) {

if (hash[arr[i]])

return true;

hash[arr[i]] = true;

}

return false;

}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn