javascript数组去重算法-----2

时间:2023-03-09 09:53:37
javascript数组去重算法-----2
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript数组去重算法-----2</title>
</head>
<body>
<script>
var arr = [1,1,2,2,3,2,2,3,3,1,1,4,4,5,6];
function unique(array){
var n = [array[0]];
for (var i = 1; i < array.length; i++) {
if (array.indexOf(array[i])==i) {
n.push(array[i])
}
}
return n
}
var cc =unique(arr);
console.log(cc)
//利用indexOf()与第一种的区别
</script>
</body>
</html>

相关文章