http://blog.csdn.net/chengxuyuan20100425/article/details/8497277

 这个方法的思路是先把数组排序,然后比较相邻的两个值。 排序的时候用的JS原生的sort方法,JS引擎内部应该是用的快速排序吧。 最终测试的结果是此方法运行时间平均是第二种方法的三倍左右,不过比第一种和第三种方法快了不少。

Array.prototype.unique4 = function()
{
    this.sort();
    var re=[this[0]];
    for(var i = 1; i < this.length; i++)
    {
        if( this[i] !== re[re.length-1])
        {
            re.push(this[i]);
        }
    }
    return re;
}

 

相关文章:

  • 2022-01-16
  • 2021-05-02
  • 2022-02-14
  • 2021-06-16
  • 2021-10-10
猜你喜欢
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
相关资源
相似解决方案