The javascript array goes to recap

  • 2021-01-22 04:51:53
  • OfStack

preface

Recently in order to change your job, prepare the interview, begin to review review JavaScript related knowledge, want to go to the array for heavy yesterday afternoon relevant methods, simply sort out a few article JavaScript algorithm, for later use, this series indefinite article number, indefinite time, does not guarantee the correctness, at the thought of which writes which does not guarantee efficiency, just talk about personal understanding, if there are any errors, please you more.

About to heavy

Array deduplication is a common algorithm research point, the way to realize the deduplication is nothing more than through the uniqueness and non-uniqueness. Basically, it's picking out the ones or deleting the ones that aren't. All of the following algorithms are named by myself, so please ignore them.

Loop match deduplication

As the name implies, it is to compare each element in the array with the array that stores the element. When the element is not repeated, it is put into the new array until the end of the loop. Because there is a loop for matching, it can also be called double loop matching to double, which is the simplest way that everyone can think of.

Implementation code:


var arr=[1,3,4,56,3,7,9,7];
var result=[];
// matching 
function isMatch(array,n){
  for(var i=0;i<array.length;i++){
    if(array[i]==n){
      return true;
    }
  }
  return false;
};
// Verify all elements 
function unqiue(array){
  for(var i=0;i<array.length;i++){
    if(!isMatch(result,array[i])){
      result.push(array[i]);
    }
  }
  return result;
};

console.log(unqiue(arr));

Note: The above method has 1 bug, and does not distinguish between numeric and numeric strings when numeric and numeric strings are present. Because the matching function isMatch() uses the double equal "==" and does not validate the element type, the congruent "===" should be used.
The modified code is as follows:


var arr=[1,3,4,56,3,'1',7,9,7];
var result=[];
// matching 
function isMatch(array,n){
  for(var i=0;i<array.length;i++){
    if(array[i]===n){
      return true;
    }
  }
  return false;
};
// Verify all elements 
function unqiue(array){
  for(var i=0;i<array.length;i++){
    if(!isMatch(result,array[i])){
      result.push(array[i]);
    }
  }
  return result;
};

console.log(unqiue(arr));

Advantages and disadvantages of the algorithm:

Advantages:

Simple implementation, intuitive thinking

Disadvantages:

inefficient

JSON de-duplication/object de-duplication/dictionary de-duplication

JSON de-duplication, in simple terms, is to convert the elements of the array to JSON or the object's key value by taking advantage of the uniformity of Object object key. value of JSON stores the index index of the array, and then loops through for in through the JSON object to store it in the new array.

Array, JSON, {} are all Object, so any one of them can be used to achieve this algorithm.

Implementation code:

Array way:


var arr=[1,3,4,56,3,'1',7,9,7];
function unqiue(array){
  var cache=[];
  var result=[];
   // Converts an array element to an object key
  for(var i=0;i<array.length;i++){
    cache[array[i]]=i;
  };
  
  // storage key( The actual array element )
  for(key in cache){
    result.push(key);
  };
  
  return result;
}
  
console.log(unqiue(arr));

JSON way:


var arr=[1,3,4,56,3,'1',7,9,7];
function unqiue(array){
  var cache={};
  var result=[];
   // Converts an array element to an object key
  for(var i=0;i<array.length;i++){
    cache[array[i]]=i;
  };
  
  // storage key( The actual array element )
  for(key in cache){
    result.push(key);
  };
  
  return result;
}
  
console.log(unqiue(arr));

Object way:


var arr=[1,3,4,56,3,'1',7,9,7];
function unqiue(array){
  var cache=new Object();
  var result=[];
   // Converts an array element to an object key
  for(var i=0;i<array.length;i++){
    cache[array[i]]=i;
  };
  
  // storage key( The actual array element )
  for(key in cache){
    result.push(key);
  };
  
  return result;
}
  
console.log(unqiue(arr));

Advantages and disadvantages of the algorithm:

Advantages:

simple

It's very efficient

Disadvantages:

1. Change array element type ()
2. bug(no more than distinguishing numeric and numeric strings)

Queue recursion is repeated

I thought about it for a long time last night. First, sort the array into ascending or descending queues, so that the same elements are in a region, and then match from the tail of the queue forward. If the match is successful, delete the tail of the queue, and then match the first element to the one before it. After the match is complete, the remaining elements are the derepeated queue.


var arr=[6, 4, 6, 9, '6', 13, 56, 9, ,'11',1, 8, '7', 17, 5, 45, 3, 7];

function unqiue(array){
  // Sort the array to form a queue 
  array.sort(function(m,n){return m-n;});
  //// After sorting, the tail of the queue is compared forward, if the same, delete the tail of the queue, and so on 
  function loop(Index){
    if(Index>=1){
      if(array[Index]===array[Index-1]){
        arr.splice(Index,1);
      }
      loop(Index-1);
    }
    
  }
  loop(array.length-1);
  return array;
}

console.log(unqiue(arr));

Advantages and disadvantages of the algorithm:

Advantages:

High efficiency

Disadvantages:

It's not the most efficient

INDEXOF to heavy mode

The new method IE8 (IE8, IE8 only supports part of ecma5) does not support indexOf


if (!Array.prototype.indexOf){ 
//  new indexOf methods  
Array.prototype.indexOf = function(item){ 
var result = -1, a_item = null; 
if (this.length == 0){ 
return result; 
} 
for(var i = 0, len = this.length; i < len; i++){ 
a_item = this[i]; 
if (a_item === item){ 
result = i; 
break; 
} 
} 
return result; 
} 
} 


Related articles: