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

How to quickly remove duplicates from an array

一个新手
一个新手Original
2017-09-07 10:28:381523browse
<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!

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
Previous article:Talk about CDN cachingNext article:Talk about CDN caching