zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    PyQt5对话框
    PyQt5基础控件
    PyQt5主界面
    PyQt5入门
    ioctl太多虚拟内存不够用
    code principles
    Error: watch ENOSPC
    intelJ
    cas
    C的函数指针的作用,以及其赋值是弱类型的
  • 原文地址:https://www.cnblogs.com/Janejxt/p/5886608.html
Copyright © 2011-2022 走看看