Summary of 6 methods for JS to achieve array deduplication

method one:

Double-layer loop, the outer loop elements, compare the values ​​in the inner loop, skip if there is the same value, push into the array if they are not.

 1 Array.prototype.distinct = function(){
 2     var arr = this,result = [],i,j,len = arr.length;
 3     for(i = 0; i < len; i++){
 4         for(j = i + 1; j < len; j++){
 5           if(arr[i] === arr[j]){
 6         j = ++i;
 7        }
 8      }
 9      result.push(arr[i]);
10   }
11   return result;
12 }
13 var arra = [1,2,3,4,4,1,1,2,1,1,1];
14 arra.distinct(); // return [3,4,2,1]    

 

Method 2: Use splice to directly operate a
double-layer loop on the original array, loop elements in the outer layer, compare the values ​​during the inner loop, and delete the value when the values ​​are the same.
Note that after deleting an element, the length of the array needs to be decremented by 1.

 1 Array.prototype.distinct = function() {
 2     var arr = this,
 3         i, j, len = arr.length;
 4     for (i = 0; i < len; i++) {
 5         for (j = i + 1; j < len; j++) {
 6             if (arr[i] == arr[j]) {
 7                 arr.splice(j, 1);
 8                 len--;
 9                 j--;
10             }
11         }
12     }
13     return arr;
14 };
15 var a = [1, 2, 3, 4, 5, 6, 5, 3, 2, 4, 56, 4, 1, 2, 1, 1, 1, 1, 1, 1, ];
16 var b = a.distinct();
17 console.log(b.toString()); //1,2,3,4,5,6,56

Advantages: simple and easy to understand
Disadvantages: high memory usage, slow speed


Method 3: Use the characteristics that the properties of objects cannot be the same to deduplicate

1 Array.prototype.distinct = function () {
 2      var arr = this ,
 3          i, obj = {},
 4          result = [],
 5          len = arr.length;
 6      for (i = 0; i < arr.length ; i++ ) {
 7          if (!obj[arr[i]]) { // If it can be found, it proves that the array elements are repeated 
8              obj[arr[i]] = 1 ;
 9              result.push(arr[i]) ;
 10          }
 11      }
 12      return result;
 13  };
14 var a = [1, 2, 3, 4, 5, 6, 5, 3, 2, 4, 56, 4, 1, 2, 1, 1, 1, 1, 1, 1, ];
15 var b = a.distinct();
16 console.log(b.toString()); //1,2,3,4,5,6,56

 

Method 4: Recursive deduplication of arrays
Use the idea of ​​recursion, sort first, then compare from the end, and delete if they are the same.

1 Array.prototype.distinct = function () {
 2      var arr = this ,
 3          len = arr.length;
 4      arr.sort( function (a, b) { // sort the array to facilitate comparison 
5          return a - b ;
 6      })
 7  
8      function loop(index) {
 9          if (index >= 1 ) {
 10              if (arr[index] === arr[index - 1 ]) {
 11                  arr.splice(index, 1 );
 12              }
 13             loop(index - 1); // Recursive loop function to deduplicate 
14          }
 15      }
 16      loop(len - 1 );
 17      return arr;
 18  };
 19  var a = [1, 2, 3, 4, 5, 6 , 5, 3, 2, 4, 56, 4, 1, 2, 1, 1, 1, 1, 1, 1, 56, 45, 56 ];
 20  var b = a.distinct();
 21 console.log (b.toString()); // 1,2,3,4,5,6,45,56

 

Method 5: Use indexOf and forEach

1 Array.prototype.distinct = function () {
 2      var arr = this ,
 3          result = [],
 4          len = arr.length;
 5      arr.forEach( function (v, i, arr) { // map is used here, The filter method can also be implemented 
6          var bool = arr.indexOf(v, i + 1); // Start from the next index value of the incoming parameter to find if there are duplicates 
7          if (bool === -1 ) {
 8              result. push(v);
 9          }
 10      })
 11      return result;
 12 };
13 var a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2, 2, 1, 23, 1, 23, 2, 3, 2, 3, 2, 3];
14 var b = a.distinct();
15 console.log(b.toString()); //1,23,2,3

 

Method 6: Use ES6's set
Set data structure, which is similar to an array, and the values ​​of its members are unique. Use Array.from to convert the Set structure into an array.

1 function dedupe(array) {
2     return Array.from(new Set(array));
3 }
4 dedupe([1, 1, 2, 3]) //[1,2,3]

Spread operator (...) uses for...of loop inside

1 let arr = [1,2,3,3];
2 let resultarr = [...new Set(arr)]; 
3 console.log(resultarr); //[1,2,3]

 

The following will give you a supplementary introduction to the method of merging arrays and removing duplicates.

1. The
idea of ​​the concat() method: The concat() method merges the incoming array or non-array value with the original array to form a new array and returns it. This method produces a new array.

1  function concatArr(arr1, arr2){
 2    var arr = arr1.concat(arr2);
 3    arr = unique1(arr); // refer to any of the above deduplication methods 
4    return arr;
 5 }

2. Array.prototype.push.apply()
idea: The advantage of this method is that a new array will not be generated.

var a = [1, 2, 3 ];
var b = [4, 5, 6 ];
Array.prototype.push.apply(a, b); // a=[1,2,3,4,5,6] 
  // equivalent to: a.push.apply(a, b); 
  // also Equivalent to [].push.apply(a, b); 
function concatArray(arr1,arr2){
   Array.prototype.push.apply(arr1, arr2);
     arr1 = unique1(arr1);
     return arr1;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325017137&siteId=291194637