输出结果如下:
a,b,a,c,c,c
a,b,c

解决方案 »

  1.   

    如果不需要顺序,可以先排序,在依次找不同的元素,放到新的数组中
    a.sort()
      

  2.   

    <SCRIPT LANGUAGE=javascript>
    <!--
    var array1=new Array();
    for(var i=0; i<1000; i++)
    {
      array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
    }
    document.write(array1+"<br>");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");
    }
    var d = new Date().getTime();
    document.write(getUnique(array1));
    d = new Date().getTime()-d;
    document.write("<br>2000节点 老算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
    document.write(getUnique2(array1));
    d = new Date().getTime()-d;
    document.write("<br>2000节点 新算法计耗时 "+ d +" 毫秒!");
    //-->
    </SCRIPT>
      

  3.   

    为array添加了一个method:unique(),采用关联结构的方式,效率很高。<script language="JavaScript">
    var array1=new Array("a","c","b","b","a","c","c","c");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(array1.unique());
    d = new Date().getTime()-d;
    document.write("<br>2000节点 新算法计耗时 "+ d +" 毫秒!");
    </script>
      

  4.   

    按梅的比较算法的结果:<SCRIPT LANGUAGE=javascript>
    <!--
    var array1=new Array();
    for(var i=0; i<1000; i++)
    {
      array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
    }
    document.write(array1+"<br>");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(getUnique(array1));
    d = new Date().getTime()-d;
    document.write("<br>2000节点 wzhiyuan (我是谁) 算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
    document.write(getUnique2(array1));
    d = new Date().getTime()-d;
    document.write("<br>2000节点 meizz(梅花雪) 算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
    document.write(array1.unique());
    d = new Date().getTime()-d;
    document.write("<br>2000节点 关联结构算法计耗时 "+ d +" 毫秒!");
    //-->
    </SCRIPT>
      

  5.   

    seeu1688(kangel) 的算法不错, 将速度又进一步提升了几十倍.
      

  6.   

    TO meizz(梅花雪) 
    我的算法比较传统,不过通用,稳定。不管数组的数据如何,结果都一样,时间也稳定(n*n )。
    你的算法我研究了一下,好在构思新颖,效率有时候也很高,不过我觉得还有改进的余地。
    第一,你用正则替换时,一次替换只是把两个相同的中的第一个替换成空,是不是可以考虑,一次替换中,把相同的只保留一个,其余所有的都替换成空呢?
    第二,用你的正则匹配时,效率高低很大程度上取决于目标串的内容,比如数组我用下面的方法构造时,你再测试一下结果!
    var array1=new Array();
    array1[0]="A";
    array1[1]="B";
    array1[2]="C";
    array1[3]="D";
    array1[4]="E";
    for(var i=5; i<1000; i++)
    {
      array1[i] = String.fromCharCode(Math.floor(Math.random()*26)+97);
    }第三,你的正则用“\w”匹配,这样对类似“ text”和"text"这样的字串,根本就认为是相同的。以上我的看法有不成熟的地方大哥再指正。TO seeu1688(kangel)
    你的我还没细看,待我研究后再讨论。欢迎各位继续探讨。
      

  7.   

    嗯, 我的方法确实有很多需要改进之外, 而使用 seeu1688(kangel) 的方法对字符型数组的效率是最高的, 且没有什么BUG. 而对于非字符型数组seeu1688(kangel)的方法和我的正则替换都不适用.
      

  8.   

    seeu1688(kangel) 的算法
    是我能想象的最强的。时间复杂度是n。个人觉得已经完美了。seeu1688(kangel),请受我一拜!
      

  9.   

    好象很久前就写过了
    http://community.csdn.net/Expert/topic/3353/3353124.xml?temp=.4150659
      

  10.   

    用VBS里的字典表和使用JS里的哈希表思路是一样的, 耗时相差不大, 不过不管是采用正则或者采用哈希采用字典表都只适用于字符型数组.
      

  11.   

    刚来csdn,以前的好贴没看过的不少。看来以后我要多抛砖引玉。
      

  12.   

    seeu1688(kangel) 的算法实在是高,佩服以下是我的
    <script>
    var array1=[];
    for(var i=0; i<1000; i++)
      array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
    document.write(array1+"<br/>");
    var t=new Date().getTime();
    s="";
    for(i=0;i<array1.length;i++)
      if((","+s).indexOf(","+array1[i]+",")==-1)
         s+=array1[i]+",";
    s=s.substring(0,s.length-1).split(",");
    document.write(new Date().getTime()-t);
    document.write(s);
    </script>
      

  13.   

    x,y,g,w,t,j,v,f,m,o,l,a,i,d,c,k,q,r,p,z,e,n,h,b,u,s
    2000节点 wzhiyuan (我是谁) 算法计耗时 313 毫秒!a,v,b,q,k,n,s,z,f,x,e,p,o,m,w,d,h,i,l,c,t,r,g,u,y,j
    2000节点 meizz(梅花雪) 算法计耗时 62 毫秒!x,y,g,w,t,j,v,f,m,o,l,a,i,d,c,k,q,r,p,z,e,n,h,b,u,s
    2000节点 scoutlin(梅川库子)算法计耗时 16 毫秒!x,y,g,w,t,j,v,f,m,o,l,a,i,d,c,k,q,r,p,z,e,n,h,b,u,s
    2000节点 关联结构算法计耗时 0 毫秒!seeu1688(kangel)实在是精彩