Home  >  Article  >  Backend Development  >  What are the 5 methods of deduplicating arrays?

What are the 5 methods of deduplicating arrays?

coldplay.xixi
coldplay.xixiOriginal
2020-06-10 11:57:023994browse

What are the 5 methods of deduplicating arrays?

What are the 5 methods to remove duplicates from arrays?

5 ways to remove duplicates from arrays:

Method 1:

Double for loop Deduplication

Principle: If two pairs are compared, delete the second one

For example: 1 1 1 3 2 1 2 4

Let the first 1 be arr [0] Compare with the following ones one by one. If the following value is equal to arr[0], delete the following value

The result after the first end is 1 3 2 2 4, delete all the following 1

Similarly, the second and third times will delete the same elements as yourself

function noRepeat1(arr){
        // 第一层for用来控制循环的次数
        for(var i=0; i<arr.length; i++){
            //第二层for 用于控制与第一层比较的元素
            for(var j=i+1; j<arr.length; j++){
                //如果相等
                if(arr[i] == arr[j]){
                    //删除后面的 即第 j个位置上的元素  删除个数 1 个
                    arr.splice(j,1);
                    // j--很关键的一步  如果删除 程序就会出错 
                    //j--的原因是 每次使用splice删除元素时 返回的是一个新的数组 
                    // 这意味这数组下次遍历是 比较市跳过了一个元素
                    /*
                        例如: 第一次删除后 返回的是 1 1 3 2 1 2 4
                     *  但是第二次遍历是 j的值为2  arr[2] = 3
                     *  相当于跳过一个元素 因此要 j--
                     * */
                    j--;
 
                }
 
            }
        }
 
        return arr;
    }

Method 2:

Single-layer for loop

Principle Similar to method one

function norepeat(arr){
                arr.sort();
                //先排序让大概相同的在一个位置,这里为什么说是大概相同 是因为sort排序是把元素当字符串排序的 它和可能排成 1 1 10 11 2 20 3 ... 不是我们想要的从小到大
                for(var i = 0; i < arr.length-1;i++){
        //还是两两比较 一样删除后面的
                    if(arr[i]==arr[i+1]){
                        arr.splice(i,1);
                        //i-- 和j--同理
                        i--;
                    }
                }
                return arr;
             }

Method three:

Principle: Use an empty array to store the first appearing element
Use the indexOf attribute indexOf to return a specified The position where the character appears in the string. If not, -1 will be returned.
So we can make good use of this property. When -1 is returned, it will be stored in the array.

function noRepeat2(arr){
        var newArr = [];
        for(var i = 0; i < arr.length; i++){
            if(newArr.indexOf(arr[i]) == -1){
                        newArr.push(arr[i]);
                }
        }
        return newArr;
        }

Method 4:

Principle: Use the idea of ​​​​the object. If there is no such attribute in the object, undefined will be returned.
Use this principle to put it into the array when the returned value is undefined. When assigning a value to this attribute

function norepeat3(arr) {
        var obj = {};
        var newArr = [];
        for(var i = 0; i < arr.length; i++) {
            if(obj[arr[i]] == undefined) {
                newArr.push(arr[i]);
                obj[arr[i]] = 1;
            }
        }
        return newArr;
     }

Method 5:

Principle: If the loop comparison is equal, the value of the following elements will be 0, and finally deleted to 0 during output The premise is that there cannot be 0 in your data, but everything can be flexible. You can set any value to replace this 0. This method was what I thought of implementing at the time, so it was not well optimized.

var newArr = [];
    //控制外循环
    for(var i=0; i<arr.length-1;i++){
        //内存循环 只比较后面的
        for(j=i+1;j<arr.length;j++){
            //如果相等就让其值等于0
            if(arr[i]==arr[j]){
                arr[j]=0;
            }
        }
        //去除值为0的
        if(arr[i]==0){
            continue;
        }else{
            //放入新的数组
            newArr.push(arr[i]);
        }
}

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of What are the 5 methods of deduplicating arrays?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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