Home >Backend Development >PHP Tutorial >How to quickly remove duplicates from an array
<body> <input type="button" value="去重" onclick="signal()"> <script> var a = [1,5,7,8,9,4,0,1,5,10]; function signal(){ //对数组排序 a.sort(); //定义一个b数组,让数组的内容等于a的第一个数 var b = [a[0]]; //遍历 for(var i = 1;i< a.length;i++){ //如果数组a的内容和数组b的不相等,则将其加入到数组b中 if(a[i] !== b[b.length-1]){ b.push(a[i]); } } alert(b); } </script> </body>
The above is the detailed content of How to quickly remove duplicates from an array. For more information, please follow other related articles on the PHP Chinese website!