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 ...
随机推荐
- cpu内存访问速度,磁盘和网络速度,所有人都应该知道的数字
google 工程师Jeff Dean 首先在他关于分布式系统的ppt文档列出来的,到处被引用的很多. 1纳秒等于10亿分之一秒,= 10 ^ -9 秒 ---------------------- ...
- idea maven项目如何使用lib下得jar包
在项目开发中,一般使用maven来管理项目,但有时还需要引用本地lib下的jar包 比如,中央仓库没有jar包.这时我们就需要引入lib下的jar包了. 1.首先在dependencies里加入本地j ...
- 配置Git绑定Git@OSC
用户名,这个名字会出现在以后的提交记录中. git config --global user.name "Git@OSC用户名" 然后是Email,同样,这个Email也会出现在你 ...
- MariaDB的存储过程和函数
创建存储过程 DELIMITER $$ DROP PROCEDURE IF EXISTS `sp_test1`; CREATE PROCEDURE sp_test1(IN a int, IN b in ...
- powx-n 分治实现乘方
题目描述 Implement pow(x, n). AC: class Solution { public: double pow(double x, int n) { && n == ...
- RabbitMQ学习笔记1-hello world
安装过程略过,一搜一大把. rabbitmq管理控制台:http://localhost:15672/ 默认账户:guest/guest RabbitMQ默认监听端口:5672 JAVA API地 ...
- 使用gprof2dot和graphivz生成程序运行调用图
使用gprof2dot和graphivz生成程序运行调用图 gprof2dot是一个将gprof生成的输出转换为dot脚本的工具.通过给定一个gprof的输出文件,将其转换为生成程序调用图的dot脚本 ...
- Retrofit2完全教程
本文注目录: Retrofit入门 Retrofit注解详解 Gson与Converter RxJava与CallAdapter 自定义Converter 自定义CallAdapter 其它说明 前言 ...
- Mac OS使用技巧十九:Safari碉堡功能之二查看网页源代码
由于大三下的时候选修了搜索技术.了解了网络上搜索引擎和网络爬虫的信息扒取的一些东西,后来我们做了一个比較水的东西.就是仅仅扒取了几家较大的下载站点几十个软件的评分下载量等信息,当用户输入一个 ...
- JS正则替换掉小括号及内容
正則表達式:\ ( [ ^ \ ) ] * \ ) JS代码: var str="hello(world)"; var nstr = str.replace(/\([^\)]*\) ...