zoukankan      html  css  js  c++  java
  • 【转】 js数组 Array 交集 并集 差集 去重

    原文:http://blog.csdn.net/ma_jiang/article/details/52672762

    最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上,网上的实现方式都是2次循环,性能不适合我的需求,1000*1000那次数太多了,所以我这里采用对象object来做处理,用空间换时间,code 如下:

    ///集合取交集  
    Array.intersect = function () {  
        var result = new Array();  
        var obj = {};  
        for (var i = 0; i < arguments.length; i++) {  
            for (var j = 0; j < arguments[i].length; j++) {  
                var str = arguments[i][j];  
                if (!obj[str]) {  
                    obj[str] = 1;  
                }  
                else {  
                    obj[str]++;  
                    if (obj[str] == arguments.length)  
                    {  
                        result.push(str);  
                    }  
                }//end else  
            }//end for j  
        }//end for i  
        return result;  
    }  
      
    //集合去掉重复  
    Array.prototype.uniquelize = function () {  
        var tmp = {},  
            ret = [];  
        for (var i = 0, j = this.length; i < j; i++) {  
            if (!tmp[this[i]]) {  
                tmp[this[i]] = 1;  
                ret.push(this[i]);  
            }  
        }  
      
        return ret;  
    }  
    //并集  
    Array.union = function () {  
        var arr = new Array();  
        var obj = {};  
        for (var i = 0; i < arguments.length; i++) {  
            for (var j = 0; j < arguments[i].length; j++)  
            {  
                var str=arguments[i][j];  
                if (!obj[str])  
                {  
                    obj[str] = 1;  
                    arr.push(str);  
                }  
            }//end for j  
        }//end for i  
        return arr;  
    }  
      
    //2个集合的差集 在arr不存在  
    Array.prototype.minus = function (arr) {  
        var result = new Array();  
        var obj = {};  
        for (var i = 0; i < arr.length; i++) {  
            obj[arr[i]] = 1;  
        }  
        for (var j = 0; j < this.length; j++) {  
            if (!obj[this[j]])  
            {  
                obj[this[j]] = 1;  
                result.push(this[j]);  
            }  
        }  
        return result;  
    };  
      
    console.log(Array.intersect(["1", "2", "3"], ["2", "3", "4", "5", "6"]));//[2,3]  
    console.log([1, 2, 3, 2, 3, 4, 5, 6].uniquelize());//[1,2,3,4,5,6]  
    console.log(Array.union(["1", "2", "3"], ["2", "3", "4", "5", "6"], ["5", "6", "7", "8", "9"]))  
    console.log(["2", "3", "4", "5", "6"].minus(["1", "2", "3"]));  
  • 相关阅读:
    Recommended Books for Algo Trading in 2020
    Market Making is simpler than you think!
    Top Crypto Market Makers of 2020
    Top Crypto Market Makers, Rated and Reviewed
    爬取伯乐在线文章(五)itemloader
    爬取伯乐在线文章(四)将爬取结果保存到MySQL
    爬取伯乐在线文章(三)爬取所有页面的文章
    爬取伯乐在线文章(二)通过xpath提取源文件中需要的内容
    爬取伯乐在线文章(一)
    爬虫去重策略
  • 原文地址:https://www.cnblogs.com/eedc/p/6705005.html
Copyright © 2011-2022 走看看