Fast Algorithm To Find Unique Items in JavaScript Array
When I had the requirement to remove duplicate items from a very large array, I found out that the classic method to be not optimized as it took a pretty long time than desired. So, I devised this new algorithm that can sort a large array in a fraction of the original time.
The fastest method to find unique items in array
This method is kind of cheeky in its implementation. It uses the JavaScript’s object to add every item in the array as key. As we all know, objects accepts only unique keys and sure we did capitalize on that.
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
ome Thoughts On This Algorithm
This is somewhat classified as “Hash Sieving” method and can also be related to a somewhat modified “Hash Sorting Algorithm” where every item in the array is a hash value and a hash function inserts item into a bucket, replacing existing values in case of hash collision. As such, this can be applied to any programming language for faster sieving of very large arrays.
This algorithm has a linear time complexity of O(2n) in worst case scenario. This is way better than what we will observe for the classic method as described below.
About the classic method
The classic (and most popular) method of finding unique items in an array runs two loops in a nested order to compare each element with rest of the elements. Consequently, the time complexity of the classic method to find the unique items in an array is around quadratic O(n²).
This is not a good thing when you have to find unique items within array of 10,000 items.
Array.prototype.unique = function() {
var a = [], l = this.length;
for(var i=0; i<l; i++) {
for(var j=i+1; j<l; j++)
if (this[i] === this[j]) j = ++i;
a.push(this[i]);
}
return a;
};
参考:http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
http://www.codeceo.com/article/javascript-delete-array.html
Fast Algorithm To Find Unique Items in JavaScript Array的更多相关文章
- Sequential Minimal Optimization: A Fast Algorithm for Training Support Vector Machines 论文研读
摘要 本文提出了一种用于训练支持向量机的新算法:序列最小优化算法(SMO).训练支持向量机需要解决非常大的二 次规划(QP)优化问题.SMO 将这个大的 QP 问题分解为一系列最小的 QP 问题.这些 ...
- [Javascript ] Array methods in depth - sort
Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...
- JavaScript Array methods performance compare
JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...
- JavaScript Array 对象
JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...
- JavaScript Array(数组)对象
一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...
- Javascript Array.prototype.some()
当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02 "mercury", 03 " ...
- Javascript Array 方法整理
Javascript Array 方法整理 Javascript 数组相关方法 说明 大多数其它编程语言不允许改变数组大小,越界访问索引会报错,但是 javascript不会报错,不过不建议直接修改a ...
- javascript array操作
首先来看一下怎么判断一个对象是不是数组: 1.Array.isArray(obj) 调用数组的isArray方法 2.obj instanceof Array 判断对象是否是Array的实例 3.Ob ...
- JavaScript : Array assignment creates reference not copy
JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...
随机推荐
- sqlalchemy 获取计数 count
from sqlalchemy import func message_count = self.db.query(func.count(Message.uid)).filter(Message.ui ...
- Scala的Class、Object和Apply()方法
Scala中如果一个Class和一个Object同名,则称Class是Object的伴生类.Scala没有Java的Static修饰符,Object下的成员和方法都是静态的,类似于Java里面加了St ...
- 《Android源代码设计模式解析与实战》读书笔记(十)
第十章.解释器模式 解释器模式是一种用的比較少的行为型模式.其提供了一种解释语言的语法或表达式的方式. 可是它的使用场景确实非常广泛,仅仅是由于我们自己非常少回去构造一个语言的文法,所以使用较少. 1 ...
- 【AIX】查看系统内存、CPU等信息
1.查看内存大小(结果单位为kb) bootinfo –r 2.查看物理CPU个数 prtconf|grep Processors 3.查看逻辑CPU个数 pmcycles –m 4.查看COU核数 ...
- 解决ajax异步传输数据,return返回为undefined的问题
function GetUserInfo(tp) { var username; $.ajax({ type: "POST", cache: false, data: " ...
- java第四节 异常/访问控制/jar包
/* 异常 异常定义了程序中遇到的非致命的错误,而不是编译时的语法错误,如程序要打开一个不存在的文件 网络连接中断,操作数越界,装载一个不存在的类等 try, catch语句 throws关键字 自定 ...
- weblogic启动报错|unable to create new native threadjava
问题描述: <-- 上午10时20分01秒 CST> <Critical> <WebLogicServer> <BEA-> <Server sub ...
- 查看Win系统激活状态
Win键+R调出运行框,在运行框中输入cmd slmgr.vbs -dlv winver 回车后就能看到当前系统的版本 slmgr.vbs -dli 显示:操作系统版本.部分产品 ...
- Apache+PHP配置PATHINFO的一个小问题
使用ThinkPHP示例,设置'URL_MODEL' => 2,发现提示: No input file specified 应该是PATHINFO配置的问题,试 ...
- 封装naive socket
周五去一个公司打了个酱油,面试官问我:你封装过socket没? 言下之意是问我实际写过底层代码没,我悻悻地说写过点. PS:说实话木有封装过,今天无聊就来封装下. 话说写了这么久C++,底层用c来写还 ...