您的位置:首页 > Web前端 > JavaScript

js数组去重

2016-03-31 15:42 686 查看
利用原生JavaScript实现数组去重

方法一

思想:创建一个新数组(结果数组),每次取原数组中的元素并检查结果数组中是否已经存在该元素,如果不存在,则将该元素加入该数组,否则,跳过该元素去检查下一个元素。

代码:

Array.prototype.unique1 = function(){
var result = [this[0]];
for(var i = 1; i < this.length; i++){
var repeat = false;
for(var j = 0; j < result.length; j++){
if(this[i] == result[j]){
repeat = true;
break;
}
}
if(!repeat){
result.push(this[i]);
}
}
return result;
};


测试:

var arr = [1,2,3,4,'hhh','hello!',222,1,3,2,4,6,111,1];
console.log(arr);
console.log(arr.unique1());


结果:



这是最容易想到的一种方法,但是却运用了两个循环,性能上不合心意

方法二

思想:

先将数组进行排序,这样数组中重复的元素会被排列到一起,检查在结果数组中是否已存在只需将该元素与结果数组的最后一个元素进行比较即可。和方法一相似,创建新的结果数组,将不重复的元素插入到结果数组中。

代码:

Array.prototype.unique2 = function(){
//先排序
this.sort();
var result = [];
result.push(this[0]);
for(var i = 1; i < this.length; i++){
if(this[i] != result[result.length-1]){
result.push(this[i]);
}
}
return result;
};
测试:

var arr = [1,2,3,4,'hhh','hello!',222,1,3,2,4,6,111,1];
console.log(arr);
console.log(arr.unique2());


结果:



在方法二中性能的问题得到了解决

但是可以注意到,这种方法中对数组进行了排序,导致了数组原来有的顺序被打乱了,如果情况不允许我们打乱原数组顺序的话,这种方法就显现除了一定的局限性。

方法三

思想:

利用一个对象来实现哈希表的思想,具体做法是每次取一个元素,到对象中去访问这个值,如果访问不到,则把该元素放入结果数组中,并把该元素值作为属性存入对象,赋值true。

代码:

Array.prototype.unique3 = function(){
var hash = {},
len = this.length,
result = [];
for(var i = 0; i < len; i++){
if(!hash[this[i]]){
hash[this[i]] = true;
result.push(this[i]);
}
}
return result;
};


测试:

var arr = [1,2,3,4,'hhh','hello!',222,1,3,2,4,6,111,1];
console.log(arr);
console.log(arr.unique3());


结果:



发现一个问题

在测试的过程中发现了一个问题

当数组中元素为以下

var arr = [1,2,3,4,'hhh','hello!','222',222,1,3,2,4,6,111,1];


输出结果为

➜ winterexcise node leecode.js
[ 1, 2, 3, 4, 'hhh', 'hello!', '222', 222, 1, 3, 2, 4, 6, 111, 1 ]
[ 1, 2, 3, 4, 'hhh', 'hello!', '222', 6, 111 ]

发现‘222’和222被当做同一个元素了!

现在对方法三做出如下改动

Array.prototype.unique3 = function(){
var hash = {},
len = this.length,
str = '',
result = [];
for(var i = 0; i < len; i++){
if(typeof this[i] == 'string'){
str = 'str_';
}else{
str = '';
}
if(!hash[str + this[i]]){
hash[str + this[i]] = true;
result.push(this[i]);
}
}
return result;
};

一个小改动,就是把字符串做一个标记,这样在对象中存的时候就不会出现原来的问题了

结果:

➜ winterexcise node leecode.js
[ 1, 2, 3, 4, 'hhh', 'hello!', '222', 222, 1, 3, 2, 4, 6, 111, 1 ]
[ 1, 2, 3, 4, 'hhh', 'hello!', '222', 222, 6, 111 ]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript