Several methods of deduplication of arrays in JavaScript

  I am a chicken, and I went to the interview two days ago, and found that it is relatively common to encounter the interview question of array deduplication again. There are several ways to achieve the effect of deduplication, and I feel it is necessary to sort out several common implementations:

  1. Idea: Create an empty array, then loop through the arrays that need to be deduplicated, compare the created arrays, if there is no current loop subscript value in the newly created array, push it to the new array, otherwise execute the next time cycle,

     code show as below:

        Array.prototype.distinct = function(){

            //Create an empty array to store the first occurrence of each element in the deduplicated array during the loop

            var resArr = [];

            for( var i = 0 ; i  < this.length ; i ++ ){

                / / Determine whether there is an element in the current loop array in the array resArr

                resArr.indexOf(this[i]) === -1?resArr.push( this [ i ] ):'';

            }

            return resArr;

        }

 

 

  2. Idea: It is still the array that needs to be deduplicated (hereinafter referred to as oldArr), and each time oldArr is looped, another loop is set to see if the elements of the outer loop array appear again in the inner loop (this time

        The starting value of the inner loop is the next element subscript of the outer loop), if it appears again, the subscript of the outer loop needs to be moved forward by one place, and the inner loop is still set to the current subscript of the outer loop. target

        previous one. If it does not appear again, it means that the subscript of the element of the current outer loop is the last same element that appears in the array, and then put it into an array, and the loop plays the whole way.

        oldArr, push the above values ​​into the array in turn, and then return.

   code show as below:

        Array.prototype.dupRem = function(){

            // empty array to store the result

            var resArr = [];

            for(var i=0;i<this.length;i++){

                  for(var j=i+1;j<this.length;j++){

                        //When the current element of the outer loop has the same element behind it, the subscript of the outer loop body is incremented by 1

                        if(this[i] === this[j]){

                            j = ++i;

                        }

                  }

                  // put the elements that do not appear in the subsequent elements into the result set array

                  resArr.push(this[i]);

            }

            return resArr;

        }

 

 

  3. Ideas: This method is actually similar to the above method. The previous one is that the result returns a new array, which will not change the original array.

    code show as below:

          Array.prototype.dupRem = function(){

              // save the length of the operation array in a variable

              var len = this.length;

              //outer loop array

              for (var i = 0; i <len; i ++) {

                  // inner loop

                  for(var j = i+1;j<len;j++){

                      //Similarly, when the same element appears in the inner loop (that is, the same element appears after the current array element), delete the element

                      if(this[i] === this[j]){

                          this,splice(j,1); //delete the same element that follows

                          len --; //After the element is deleted, the len that identifies the length of the array is also decremented by 1

                          j --; //After the element is deleted, in order to ensure the completeness of the loop, the subscript of the inner loop is reduced by 1,

                      }

                  }

              }

              return this; //return the original array after deduplication

          }

 

    Problem: occupy the inner layer, the speed is slow.

 

  4. Ideas: Use the characteristics of object attributes that cannot be repeated to remove duplicates. Create a new empty object, put the array element as the object key name into the new empty object, and push the attribute name to an object every time a new attribute is added to the object.

       In the array, this number is finally returned, and the result is the deduplicated array

 

    code show as below:

          Array.prototype.dupRem = function(){

              var len = this.length, // save the array elements

                arr = [], //Array to store results

                obj = {}; //Object used to judge repetition

              for (var i = 0; i <len; i ++) {

                  //Determine whether there is an attribute with the current loop subscript as the key name in the object. If it does not exist, it is the first occurrence (ignored when the same element appears in the subsequent comparison)

                  if(!obj[this[i]]){

                      obj[this[i]] = 'any value';

                      arr.push = this[i];

                  }

              }

              //return the result array after deduplication

              return arr;

          }

 

  5. Use ES6's set data structure:

        function dopRem(arr){

            return Array.from(new Set(arr));

        }

 

  

Guess you like

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