Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:25:26
Original
1428 people have browsed it

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;

}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template