Home  >  Article  >  Web Front-end  >  How to implement array deduplication in JavaScript? Three methods to remove duplication from arrays in js (code examples)

How to implement array deduplication in JavaScript? Three methods to remove duplication from arrays in js (code examples)

青灯夜游
青灯夜游Original
2018-10-15 13:37:303895browse

How to implement array deduplication in JavaScript? This article will introduce to you three common methods of deduplicating arrays in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Below we will introduce three common methods of array deduplication in js through simple code examples.

The first method: for loop (twice) new array

Idea:

1. Construct a new array to store the result

2. Take out one element from the original array each time in the for loop, and use this element to loop and compare it with the result array

3. If the result array does not contain the element element, then store it in the result array

//方法一
var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
function removeDuplicatedItem(arr) {
   for(var i = 0; i < arr.length-1; i++){
       for(var j = i+1; j < arr.length; j++){
           if(arr[i]==arr[j]){
              arr.splice(j,1);//console.log(arr[j]);
              j--;
           }
       }
   }
   return arr;
}

arr2 = removeDuplicatedItem(arr);
console.log(arr);
console.log(arr2);

The first method can basically meet our needs, for arr = [1,23,1,1,1,3,23,5,6, For a simple array like 7,9,9,8,5], you only need to use type comparison, but what if the array is very long? Traversing the array in this way, the length of the array is n, then the time complexity is n*n. Obviously the performance of the first method needs to be improved. Next is the second method, which uses array sorting to remove duplicate values ​​during the sorting process.

The second method: for loop (once) sort() sorts the new array

The length of the original array remains unchanged but is sorted by string Sequential sorting, with the help of the new array, determine whether the element exists in the new array. If it does not exist, add the element to the new array

//方法二
var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
function removeRepEle(ar) {
    var ret = [],
        end;//临时变量用于对比重复元素
    ar.sort();//将数重新组排序
    end = ar[0];
    ret.push(ar[0]);
    for (var i = 1; i < ar.length; i++) {
        if (ar[i] != end) {//当前元素如果和临时元素不等则将此元素添加到新数组中
            ret.push(ar[i]);
            end = ar[i];
        }
    }
    return ret;
}

arr2 = removeRepEle(arr);
console.log(arr);//[ 1, 1, 1, 1, 23, 23, 3, 5, 5, 6, 7, 8, 9, 9 ]
console.log(arr2);//[ 1, 23, 3, 5, 6, 7, 8, 9 ]

The second method has certain limitations, because the array is sorted first , and then remove duplicates, so the final returned result is the result of js array deduplication and sorting. If it is required to remove duplicates without changing the order of the array, then this method is not advisable.

The third method (recommended): for loop (once) new array new object

Use empty objects to record what has been in the new array Stored elements

//方法三 
var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
var o={};
var new_arr=[];
for(var i=0;i<arr.length;i++){
    var k=arr[i];
    if(!o[k]){
        o[k]=true;
        new_arr.push(k);
    }
}
console.log(new_arr);

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more related tutorials, please visit JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of How to implement array deduplication in JavaScript? Three methods to remove duplication from arrays in js (code examples). 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