Home > Web Front-end > JS Tutorial > body text

Remove duplicate data from JS array

小云云
Release: 2018-03-13 16:08:19
Original
1549 people have browsed it

In JS, we often encounter the need to remove duplicate data from arrays. Here we introduce four algorithms to achieve the function of deduplicating JS arrays. I hope it can help everyone.

1. The fastest algorithm: object key-value pair method

Implementation idea: create a new js object and a new array, and when traversing the incoming array, determine whether the value is a js object key, if not, add the key to the object and put it into a new array.

//注意点: 判断 是否为js对象键时,会自动对传入的键执行“toString()”,不同的键可能会被误认为一样;例如: a[1]、a["1"] 。解决上述问题还是得调用“indexOf”。
        //速度最快, 占空间最多(空间换时间)
        function unique(array){
            var n = {}, r = [], len = array.length, val, type;
            for (var i = 0; i < array.length; i++) {                val = array[i];                type = typeof val;                if (!n[val]) {
                    n[val] = [type];
                    r.push(val);
                } else if (n[val].indexOf(type) < 0) {
                    n[val].push(type);
                    r.push(val);
                }
            }            return r;
        }
Copy after login
Copy after login

2. The most ingenious algorithm: optimized array traversal method

Implementation idea: get the rightmost value without repetition and put it into a new array. (When duplicate values ​​are detected, the current loop is terminated and the next round of judgment of the top-level loop is entered)

function unique1(array){
            var r = [];            for(var i = 0, l = array.length; i<l; i++){                for(var j = i + 1; j < l; j++)                    if(array[i] == array[j]) j == ++i;
                r.push(array[i]);
            }            return r;
        }
Copy after login
Copy after login

3. Algorithm: adjacent removal method after sorting

Implementation idea: pass in the array Sort, after sorting, the same values ​​are adjacent, and then when traversing, only add values ​​that are not duplicates of the previous value to the new array.

//将相同的值相邻,然后遍历去除重复值
        function unique2(array){
            array.sort();            var re=[array[0]];            for(var i = 1; i < array.length; i++){                if( array[i] !== re[re.length-1])
                {
                    re.push(array[i]);
                }
            }            return re;
        }
Copy after login

4. Algorithm: Array Subscript Judgment Method

Implementation idea: If the i-th item of the current array first appears in a position other than i in the current array, then it means the i-th item It's a duplicate, so ignore it. Otherwise, store the result array

function unique3(array){
            var n = [array[0]]; //结果数组
            //从第二项开始遍历
            for(var i = 1; i < array.length; i++) {                //如果当前数组的第i项在当前数组中第一次出现的位置不是i,
                //那么表示第i项是重复的,忽略掉。否则存入结果数组
                if (array.indexOf(array[i]) == i) n.push(array[i]);
            }            return n;
        }
Copy after login
Copy after login

                                                                                                                                                 

In JS, we often encounter the need to remove duplicate data in arrays. Here we introduce four algorithms to achieve the function of deduplicating JS arrays.

1. The fastest algorithm: object key-value pairing method

Implementation idea: Create a new js object and a new array. When traversing the incoming array, determine whether the value is the key of the js object. If not, Add the key to the object and put it into a new array.

//注意点: 判断 是否为js对象键时,会自动对传入的键执行“toString()”,不同的键可能会被误认为一样;例如: a[1]、a["1"] 。解决上述问题还是得调用“indexOf”。
        //速度最快, 占空间最多(空间换时间)
        function unique(array){
            var n = {}, r = [], len = array.length, val, type;
            for (var i = 0; i < array.length; i++) {                val = array[i];                type = typeof val;                if (!n[val]) {
                    n[val] = [type];
                    r.push(val);
                } else if (n[val].indexOf(type) < 0) {
                    n[val].push(type);
                    r.push(val);
                }
            }            return r;
        }
Copy after login
Copy after login

2. The most ingenious algorithm: optimized array traversal method

Implementation idea: get the rightmost value without repetition and put it into a new array. (When duplicate values ​​are detected, the current loop is terminated and the next round of judgment of the top-level loop is entered)

function unique1(array){
            var r = [];            for(var i = 0, l = array.length; i<l; i++){                for(var j = i + 1; j < l; j++)                    if(array[i] == array[j]) j == ++i;
                r.push(array[i]);
            }            return r;
        }
Copy after login
Copy after login

3. Algorithm: adjacent removal method after sorting

Implementation idea: pass in the array Sort, after sorting, the same values ​​are adjacent, and then when traversing, only add values ​​that are not duplicates of the previous value to the new array.

//将相同的值相邻,然后遍历去除重复值
        function unique2(array){
            array.sort();            var re=[array[0]];            for(var i = 1; i < array.length; i++){                if( array[i] !== re[re.length-1])
                {
                    re.push(array[i]);
                }
            }            return re;
        }
Copy after login

4. Algorithm: Array Subscript Judgment Method

Implementation idea: If the i-th item of the current array first appears in a position other than i in the current array, then it means the i-th item It's a duplicate, so ignore it. Otherwise, store the result array

function unique3(array){
            var n = [array[0]]; //结果数组
            //从第二项开始遍历
            for(var i = 1; i < array.length; i++) {                //如果当前数组的第i项在当前数组中第一次出现的位置不是i,
                //那么表示第i项是重复的,忽略掉。否则存入结果数组
                if (array.indexOf(array[i]) == i) n.push(array[i]);
            }            return n;
        }
Copy after login
Copy after login

Related recommendations:

php mysql million-level data removal of duplicate data

The above is the detailed content of Remove duplicate data from JS array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!