js数组去重
2016-03-31 15:42
686 查看
利用原生JavaScript实现数组去重
代码:
测试:
结果:
这是最容易想到的一种方法,但是却运用了两个循环,性能上不合心意
先将数组进行排序,这样数组中重复的元素会被排列到一起,检查在结果数组中是否已存在只需将该元素与结果数组的最后一个元素进行比较即可。和方法一相似,创建新的结果数组,将不重复的元素插入到结果数组中。
代码:
结果:
在方法二中性能的问题得到了解决
但是可以注意到,这种方法中对数组进行了排序,导致了数组原来有的顺序被打乱了,如果情况不允许我们打乱原数组顺序的话,这种方法就显现除了一定的局限性。
利用一个对象来实现哈希表的思想,具体做法是每次取一个元素,到对象中去访问这个值,如果访问不到,则把该元素放入结果数组中,并把该元素值作为属性存入对象,赋值true。
代码:
测试:
结果:
当数组中元素为以下
输出结果为
➜ 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 ]
方法一
思想:创建一个新数组(结果数组),每次取原数组中的元素并检查结果数组中是否已经存在该元素,如果不存在,则将该元素加入该数组,否则,跳过该元素去检查下一个元素。代码:
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 ]
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享