javascript数组去重算法-----2
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>javascript数组去重算法-----2</title> 6 </head> 7 <body> 8 <script> 9 var arr = [1,1,2,2,3,2,2,3,3,1,1,4,4,5,6]; 10 function unique(array){ 11 var n = [array[0]]; 12 for (var i = 1; i < array.length; i++) { 13 if (array.indexOf(array[i])==i) { 14 n.push(array[i]) 15 } 16 } 17 return n 18 } 19 var cc =unique(arr); 20 console.log(cc) 21 //利用indexOf()与第一种的区别 22 </script> 23 </body> 24 </html>
坚持下去就能成功