javascript数组去重算法-----4(另一种写法)

 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的另外一种写法;

 

posted @ 2015-10-09 22:53  挥刀  阅读(160)  评论(0编辑  收藏  举报