Home > Backend Development > PHP Tutorial > How to quickly remove duplicates from an array

How to quickly remove duplicates from an array

一个新手
Release: 2023-03-15 19:42:02
Original
1524 people have browsed it
<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>
Copy after login

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!

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