首页 > 代码库 > JS实现字符串去重,数组去重

JS实现字符串去重,数组去重

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>去重</title> 6 </head> 7 <body> 8     <script type="text/javascript"> 9     /*数组去重*/10         function quchong(arr){11             var len = arr.length;12             arr.sort();13             for(var i=len-1;i>0;i--){14                 if(arr[i]==arr[i-1]){15                     arr.splice(i,1);16                 }17             }18             return arr;19         }20         var a = ["a","a","b",b,c,c,a,d];21         var b = quchong(a);22         console.log(b);23     /*字符串去重*/24     function quchongstr(str){25         var a = str.match(/\S+/g);//等价于str.split(/\s+/g)//  \s空白符,\S非空白符26         a.sort();27         for(var i=a.length-1;i>0;i--){28             if(a[i]==a[i-1]){29                 a.splice(i,1);30             }31         }32         return a.join(" ");33     }34     var str = quchongstr("a a b a b e");35     console.log(str);36 37     </script>38 </body>39 </html>

 

JS实现字符串去重,数组去重