Detail the javascript array deweighting problem

  • 2020-10-07 18:32:08
  • OfStack

The first thing That comes to mind is to create another result array to store the non-duplicate data in the original array. Iterate through the original array and compare it to the elements in the resulting array, checking for duplicates. As a result, I wrote A:


Array.prototype.clearRepetitionA = function(){
   var result = [];
   var isRepetition;
   for(var i=0; i<this.length; i++){
     isRepetition = false;
     for(var j=0; j<result.length; j++){
       if(this[i] === result[j]){
         isRepetition = true;
         break;
       }
     }
     if(!isRepetition){
       result.push(this[i]);
     }
   }
   return result;
 } 

After writing this, it occurred to me that the array method indexOf in ECMAScript 5, which I just read the other day, retrieves array elements. Instead of a layer 2 loop, I used the indexOf method and wrote the following code, B:


Array.prototype.clearRepetitionB = function(){
   var result = [];
   for(var i=0; i<this.length; i++){
     if(result.indexOf(this[i]) == -1){
       result.push(this[i]);
     }
   }
   return result;
 } 

Code 1 now goes from 17 lines to 9 lines, which is a lot cleaner. There's more than one way to solve a senior 3 math problem, and Then I go on to think of other ways. indexOf methods mean search with the given value of the elements in the array, returns to find the first element of the index, didn't find it returns 1, the first is to search the values of the parameters, the optional second parameter, which specifies an array of one index, search from there, if you omit this parameter, the search from the beginning. Thinking 1 diverged, thinking that the previous method is to detect whether the value is repeated, now with the indexOf method can not be detected by the first occurrence of each element index and the element's own index is equal to determine whether the duplicate value. So, I wrote C again:


 Array.prototype.clearRepetitionC = function(){
   var result = [this[0]];
   for(var i=1; i<this.length; i++){
     if(this.indexOf(this[i]) == i){
       result.push(this[i]);
     }
   }
   return result;
 } 

After writing this, I went on to think about it. I really can't think of any other methods. These three methods are all very basic methods. So I checked myself against the answers. 1 look at the answer, found himself or too weak, simple questions or some whimsy. The following is not my own thought, I will not say too much about my journey. Needless to say, directly on the classical answer + analysis.
First of all, let's talk about the space for time solution often used in 1 algorithm, keep formation, let's call it code D:


Array.prototype.clearRepetitionD = function(){
   var result = [];
   var obj = {};
   var key,type;
   for(var i=0; i<this.length; i++){
     key = this[i];
     type = typeof key;
     if(!obj[key]){
       obj[key] = [type];
       result.push(key);
     }else if(obj[key].indexOf(type)){
       obj[key].push(type);
       result.push(key);
     }
   }
   return result;
 } 

This method USES the attributes of 1 object obj to hold the values of the elements in the original array while traversing the original array. The value of the attribute is also an array that stores the type of the attribute, which separates elements like the number 1 from elements like the string '1' in the original array. This method reduces the time of the indexOf method of the above three methods by building an additional object, which is arguably more efficient.
If you are satisfied with the above efficient method of trading space for time and do not continue to watch, then it is a mistake, the best is yet to come. Now it's time to get started, without a doubt, with the code E:


 Array.prototype.clearRepetitionE = function(){
   var result = [];
   for(var i=0; i<this.length; i++){
     for(var j=i+1; j<this.length; j++){
       if(this[i] === this[j]){
         j = ++i;
       }
     }
     result.push(this[i]);
   }
   return result;
 }

Code D trades space for time, feeling just like 1. What about code E? This code is wrong, can this really be reweighted? Yes, I didn't understand the code at first, but it took me a second time to understand it after I read the parse. The first layer traverses the original array from front to back. The second layer loops to detect whether each element repeats with the element after it. If there is a duplicate element after it, it will be skipped. If the element is not repeated by any other element, it is added to the result array. This method also optimizes layer 2 loops and is more efficient than the first method, but the order of the elements in the result array is not the same as the order of the elements in the original array.

After reading the E code, are you already holding out your thumb and looking in admiration? Don't give me these flowers and honors. They should go to the god who wrote this method. Here's the last one: Sort first, then redo. As usual, it's called the code F:


Array.prototype.clearRepetitionF = function(){
   this.sort();
   var result = [this[0]];
   for(var i=1; i<this.length; i++){
     if(this[i] !== result[result.length-1]){
       result.push(this[i]);
     }
   }
   return result;
 } 

The array sorting method, sort, sorts the array elements before reworking them.

The above is a step by step study of javascript array de-weighting problem, constantly improve the code, a total of six pieces of code Shared, I hope you can study seriously, can harvest.


Related articles: