Recently thinking about changing jobs, went to a public company in Zhongguancun, the interviewer casually asked a few questions which mentioned how to delete the array element method, that this is the third time encountered this problem, because the knowledge of oral said so casually said, Before the use of the time are directly looking for the method of the library although know roughly how to write, but have not really written, online method or quite a lot of here also to share a writer wrote it, hope to help beginners:
//method of the array de-weightArray.prototype.unique=function(){ //declaring variables centrally varOldarr= This, NewArr=[oldarr[0]], Len=Oldarr.length, I=1; //filter an empty array if(!len)return This; //Filter repeating elements for(; i<len;i++) {_this=Oldarr[i]; Newarr.indexof (_this)<0? Newarr.push (_this): "; } //returns the filtered array without affecting the original array returnNEWARR;}varArr=[' A ', ' a ', ' B ', ' A ', ' C ', ' d '];console.log (Arr.unique ());//["A", "B", "C", "D", Unique:function]
Although there are many online, but also do not write their own, but after all, the logic of their own clearly can also follow the logic extension, such as extending to the object element to the weight or can operate multiple arrays at the same time here and then put on someone else's write several methods can be combined under the comparison
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 }
Original source: http://blog.sina.com.cn/s/blog_77a4568a0101d4lq.html
Method 2:
Iterate through the array to be deleted arr, put the elements into another array, TMP, in order to determine that the element does not exist in ARR to allow the use of the TMP two functions: for ... in and IndexOf ()
varStudent = [' Qiang ', ' Ming ', ' Tao ', ' Li ', ' Liang ', ' You ', ' Qiang ', ' Tao ']; functionUnique (arr) {//traverse arr to place elements into the TMP array (not present) varTMP =NewArray (); for(varIincharr) { //The element does not exist inside TMP to allow append if(Tmp.indexof (Arr[i]) ==-1){ } } returntmp; }
Method 3:
The target array of arr's element values and the position of the key is automatically replaced by the duplicate elements of the deleted, after the change of appearance: Array (' Qiang ' =>1, ' Ming ' =>1, ' Tao ' =>1)
<script type= "Text/javascript" >varStudent = [' Qiang ', ' Ming ', ' Tao ', ' Li ', ' Liang ', ' You ', ' Qiang ', ' Tao ']; functionUnique (arr) {varTMP =NewArray (); for(varMincharr) {Tmp[arr[m]]=1; } //re-swap the position of the key and the value again varTmparr =NewArray (); for(varNinchtmp) {Tmparr.push (n); } returnTmparr; }</script>
Method 4
/** * Remove duplicate elements of an array*/ functionUniquearray (data) {data= Data | | []; varA = {}; for(vari=0; i<data.length; i++) { varv =Data[i]; if(typeof(A[v]) = = ' undefined ') {A[v]= 1; } }; Data.length=0; for(varIincha) {Data[data.length]=i; } returndata; }
Methods are almost the third method idea is pretty clever ~
Front-end Interview High frequency problem: multiple ways to delete array repeating elements