1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>javascript数组去重算法-----3</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 array.sort() 12 var n = [array[0]]; 13 for (var i = 1; i < array.length; i++) { 14 if (array[i] != n[n.length-1]) { 15 n.push(array[i]) 16 } 17 } 18 return n 19 } 20 var cc =unique(arr); 21 console.log(cc) 22 //利用sort排序,之后去重, 23 </script> 24 </body> 25 </html>
javascript数组去重算法-----4的另外一种写法;
时间: 2024-10-27 12:18:51