javascript removes a method summary of duplicate elements in an array

  • 2020-06-19 09:45:10
  • OfStack

This article illustrates how javascript removes duplicate elements from an array. Share to everybody for everybody reference. The specific analysis is as follows:

Here to share 1 front end interview questions, the main implementation of javascript to remove the array of duplicate elements. Hope to be helpful for beginners


// Array de-weighting method 
Array.prototype.unique=function(){
  // Centrally declared variables 
  var 
   oldArr=this,
   newArr=[oldArr[0]],
   len=oldArr.length,
   i=1;
  // Filter empty array 
  if(!len) return this;
  // Filter duplicate elements 
  for(;i<len;i++){
    newArr.indexOf(oldArr[i])<0 ? newArr.push(_this) : ''; 
  }
  // Returning the filtered array does not affect the original array 
  return newArr;
}
var arr=['a','a','b','a','c','d'];
console.log(arr.unique());
//["a", "b", "c", "d", unique: function]

Although there are a lot of online and their own writing is not so good, but after all, the logic of their own writing can also follow the logical extension, such as the extension to the object elements to re-or can operate multiple arrays at the same time, etc. Here again put others to write a few methods can be compared comprehensively

Method 1:


function oSort(arr)
{
  var result ={};
  var newArr=[];
  for(var i=0;i
  {
 if(!result[arr[i]])
 {
   newArr.push(arr[i])
   result[arr[i]]=1
 }
  }
  return newArr
}

Method 2:

Iterate through the array arr to be deleted, put the elements into another array tmp, and only put them into tmp if the element does not exist in arr
Two functions are used: for... in and indexOf ()


var student = ['qiang' . 'ming' . 'tao' . 'li' . 'liang' . 'you' . 'qiang' . 'tao'];
 function unique(arr){
   //  traverse arr And put the elements into each one tmp An array of ( Let it go when it doesn't exist )
   var tmp = new Array();
   for(var i in arr){
  // The elements in the tmp An append is not allowed unless it is internally present 
  if(tmp.indexOf(arr[i])==-1){
  }
 }
  return tmp;
}

Method 3:

Swapping the element value and key position of the target array arr automatically removes duplicate elements. The swapping looks like this: array('qiang'=) > 1, 'ming = > 1, 'tao = > 1)


<script type="text/javascript">
  var student = ['qiang','ming','tao','li','liang','you','qiang','tao'];
  function unique(arr){
    var tmp = new Array();
    for(var m in arr){
      tmp[arr[m]]=1;
    }
    // Switch the keys and values again 
    var tmparr = new Array();
    for(var n in tmp){
     tmparr.push(n);
    }
   return tmparr;
 }
</script>

Methods 4


/**
*  Remove duplicate elements from the array 
*/
function uniqueArray(data){ 
  data = data || []; 
  var a = {}; 
  for (var i=0; i<data.length; i++) { 
    var v = data[i]; 
    if (typeof(a[v]) == 'undefined'){ 
      a[v] = 1; 
    } 
  }; 
  data.length=0; 
  for (var i in a){ 
    data[data.length] = i; 
  } 
  return data; 
} 

The third method idea is pretty smart ~

I hope this article has been helpful for your javascript programming.


Related articles: