Maison > Article > interface Web > Méthodes de déduplication/recherche/insertion/suppression de tableaux JavaScript
//---------------first--------------- Array.prototype.distinct = function(){ var arr=[]; var obj={}; //对象承接 for(var i=0,len=this.length;i<len;i++){ if(!obj[this[i]]){ obj[this[i]]=1; arr.push(this[i]); } } return arr; }; var arr = new Array(); arr.push(1,3,2,4,4,4,5,6,7,8,8,8,9,0); //--------------second---------------- Array.prototype.distinct = function(){ var arr=[]; for(var i=0,len=this.length;i<len;i++){ if(this[i]==this[i+1]){ continue; }else{ arr.push(this[i]); } } return arr; }; var arr = new Array(); arr.push(1,3,2,4,4,4,5,6,7,8,8,8,9,0); //----------------third---------------- Array.prototype.distinct = function(){ for(var i=0,len=this.length;i<len;i++){ for(var j=i+1;j<=len;j++){ if(this[i]==this[j]){ console.log(this[j]); this.splice(j,1); j--; //得考虑如果删除一个元素,j的值需要减1 len--; } } } //return this; }; var arr = new Array(); arr.push("a","b","c","c","b","bb","dd","d","aa","c","bb"); arr.distinct();
function removeRepeat(arr){ var array=[]; var obj={}; for(var i=0,len=arr.length;i<len;i++){ if(!obj[arr[i]]){ obj[arr[i]]=1; array.push(arr[i]); } } return array; }; var arr = new Array(1,3,2,4,4,4,5,6,7,8,8,8,9,0); var array = removeRepeat(arr);
function SequenceSearch(arr, value){ var i; for(i=0; i<arr.length; i++){ if(arr[i]==value) return i; } return -1; }
Les éléments de recherche binaires doivent être dans l'ordre, s'ils ne sont pas ordonnés, l'opération de tri doit être effectuée en premier
-------------折半查找-------------- function BinarySearch1(arr, value){ var low, high, mid; low = 0; high = arr.length-1; while(low<=high){ mid = Math.floor((low+high)/2); if(a[mid]==value){ return mid; } if(a[mid]>value){ high = mid-1; } if(a[mid]<value){ low = mid+1; } } return -1; } -------------递归版本-------------- function BinarySearch2(arr, value){ var low, high, mid; low = 0; high = arr.length-1; var mid = low+(high-low)/2; if(arr[mid]==value){ return mid; } if(a[mid]>value){ return BinarySearch2(arr, value); } if(a[mid]<value){ return BinarySearch2(arr, value); } }
//----------------first---------------- function Insert(arr, n, data){ //若插入数据位置不在表尾 if (n < arr.length){ //将要插入位置之后元素依次向后移动一位 for (var i = seqList.ListLen - 1; i >= n; i--) { seqList.ListData[i + 1] = seqList.ListData[i]; } } //将数据插入到位置为n的位置并将数组的长度加1 seqList.ListData[n-1] = data; seqList.ListLen++; return true; } //----------------second---------------- function aa(arr, val) { for (var i = 0; i <arr.length; i++) { if (arr[i] == val) return i; } return -1; }; function bb(arr, val) { var index =arr.indexOf(val); if (index > -1) { arr.splice(index, 1); } }; var emp = ['abs','dsf','sdf','fd'] bb(emp, 'fd');
function Delete(arr, n){ //判断数组是否为空 if (seqList.ListLen == 0) return false; //判断n的位置是否合法 if (n < 1 || n > seqList.ListLen) return false; //如果删除不是最后位置 if (n < seqList.ListLen) { //将删除位置后继元素依次前移 for (int i = n; i < seqList.ListLen; i++) { seqList.ListData[i-1] = seqList.ListData[i]; } } //表长减1 seqList.ListLen--; return true; }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!