javascript数组去重算法-----1
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>javascript数组去重算法-----1</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 = []; 12 for (var i = 0; i < array.length; i++) { 13 if (n.indexOf(array[i])==-1) { 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>
坚持下去就能成功