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] != array[i-1]) { 15 n.push(array[i]) 16 } 17 } 18 return n 19 } 20 var cc =unique(arr); 21 console.log(cc) 22 //利用sort排序,4的另一种写法,和4的第一种类似; 23 </script> 24 </body> 25 </html>
时间: 2024-10-11 07:26:24