zoukankan      html  css  js  c++  java
  • 清除字符串数组中,重复元素

    一、JS实现(收藏)

    <script language="JavaScript">
    <!--
    var arrData=new Array();
    for(var i=0; i<1000; i++)
    {
    arrData[arrData.length] 
    = String.fromCharCode(Math.floor(Math.random()*26)+97);
    }

    //document.write(arrData+"<br/>"); 

    //方法一,普通遍历
    function myArray_Unique(myArray)
    {
    //var myArray=new Array("a","a","c","a","c","d","e","f","f","g","h","g","h","k");
    var haha=myArray;
    for(var i=0;i<myArray.length;i++)
    {
    for(var j=0;j<myArray.length;j++)
    {
    temp
    =myArray[i];
    if((i+j+1)<myArray.length&&temp==myArray[i+j+1]) //如果当前元素与后一个元素相等
    haha.splice(i+j+1,1); //然后就移除下一个元素 
    }

    }

    return haha;
    }
     

    //方法二
    function getUnique(someArray)
    {
    tempArray
    =someArray.slice(0);//复制数组到临时数组
    for(var i=0;i<tempArray.length;i++)
    {
    for(var j=i+1;j<tempArray.length;)
    {
    if(tempArray[j]==tempArray[i])
    //后面的元素若和待比较的相同,则删除并计数;
    //
    删除后,后面的元素会自动提前,所以指针j不移动
    {
    tempArray.splice(j,
    1);
    }

    else
    {
    j
    ++;
    }

    //不同,则指针移动
    }

    }

    return tempArray;
    }
     

    //方法三 正则表达式 -- 适用于字符型数组
    function getUnique2(A)
    {
    var str = "\x0f"+ A.join("\x0f");
    while(/(\w+)[^\1]*\1/.test(str))
    str 
    = str.replace("\x0f"+ RegExp.$1"");
    return str.substr(1).split("\x0f");
    }
     

    //方法四 关联结构
    Array.prototype.unique = array_unique;
    function array_unique()
    {
    var o = new Object();
    for (var i=0,j=0; i<this.length; i++)
    {
    if (typeof o[this[i]] == 'undefined')
    {
    o[
    this[i]] = j++;
    }

    }

    this.length = 0;
    for (var key in o)
    {
    this[o[key]] = key;
    }

    return this;
    }
     

    var d = new Date().getTime();
    document.write(myArray_Unique(arrData));
    = new Date().getTime()-d;
    document.write(
    "<br/>2000元素 方法一算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约370ms~390ms左右 

    var d = new Date().getTime();
    document.write(getUnique(arrData));
    = new Date().getTime()-d;
    document.write(
    "<br/>2000元素 方法二算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约360ms~380ms左右 

    var d = new Date().getTime();
    document.write(getUnique2(arrData));
    = new Date().getTime()-d;
    document.write(
    "<br/>2000元素 正则表达式 方法三算法计耗时 "+ d +" 毫秒!<br/><br/>");//大约80ms左右 

    var d = new Date().getTime();
    document.write(arrData.unique());
    = new Date().getTime()-d;
    document.write(
    "<br/>2000元素 关联结构 方法四算法计耗时 "+ d +" 毫秒!<br /><br />");//大约0ms~10ms左右 

    //-->
    </script> 

    二、巧用.net的NameValueCollection实现(原创)

     

    /// <summary>
    /// 删除字符串组中相同元素
    /// </summary>
    /// <param name="strArr"></param>
    /// <returns></returns>

    public static string[] GetUnique(string[] strArr)
    {
    System.Collections.Specialized.NameValueCollection name 
    = new System.Collections.Specialized.NameValueCollection(); 

    foreach (string s in strArr)
    {
    name[s] 
    = s;
    }

    return name.AllKeys;
    }
     
  • 相关阅读:
    Nginx负载均衡配置
    云鹏在美团八年工作总结
    尽量不要让儿女从事这3种工作,钱再多也别做,坚持再久也没前途
    Nginx 配置ip_hash
    postgresql获取表结构,表名、表注释、字段名、字段类型及长度和字段注释
    更快的Maven来了,我的天,速度提升了8倍!
    Nginx负载均衡配置及算法详解
    nginx负载均衡策略:ip_hash、url_hash
    Windows Phone实用开发技巧(41):解决WebBrowser中显示黑色背景网页闪屏
    Windows Phone实用开发技巧(32):照片角度处理
  • 原文地址:https://www.cnblogs.com/CSharp/p/477406.html
Copyright © 2011-2022 走看看