zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    矩阵解压,网络流UESTC-1962天才钱vs学霸周2
    最小生成树唯一性判断-UESTC1959天才钱vs学霸周
    度及拓扑图的使用-UESTC1958学霸周选课
    CodeForces1000A-Light It Up
    CodeForces1000A- Codehorses T-shirts
    CoderForces999F-Cards and Joy
    CoderForces999E-Reachability from the Capital
    CoderForces999D-Equalize the Remainders
    CoderForces999C-Alphabetic Removals
    CoderForces999B- Reversing Encryption
  • 原文地址:https://www.cnblogs.com/duhuo/p/5807969.html
Copyright © 2011-2022 走看看