Array deduplication is a common requirement, and we temporarily consider arrays of the same type to deduplication. The main thing is to clarify the ideas and consider the performance. The following methods are basically available online, and I will just briefly summarize them here.
Ideas:
1. Iterate through the array, compare one by one, and delete the following one after the comparison to the same one.
2. Iterate over the array, compare one by one, compare to the same one, skip the previous repeated ones, and put them into the new array if they are different.
3. Take any array element into a new array, traverse any remaining array element, compare it with the elements of the new array one by one. If there is a difference, put it into the new array.
4. Iterate over the array, take an element, as the object's attribute, and determine whether the attribute exists
1. Delete the duplicates afterwards:
function ov1(arr){ //var a1=((new Date).getTime()) for(var i=0;i<arr.length;i++) for(var j=i+1;j<arr.length;j++) if(arr[i]===arr[j]){arr.splice(j,1);j--;} //console.info((new Date).getTime()-a1) return arr.sort(function(a,b){return ab});}2. This is a conventional method, which is easier to understand. If the same, the loop will break out.
function ov2(a) { //var a1=((new Date).getTime()) var b = [], n = a.length, i, j; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) if (a[i] === a[j]){j=false;break;} if(j)b.push(a[i]); } //console.info((new Date).getTime()-a1) return b.sort(function(a,b){return ab});}3. It took me a long time to understand this. Although the j loop continues here, the i value has changed. It's equivalent to a new i loop:
function ov3(a) { //var a1=((new Date).getTime()) var b = [], n = a.length, i, j; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) if (a[i] === a[j])j=++i b.push(a[i]);} //console.info((new Date).getTime()-a1) return b.sort(function(a,b){return ab});}4. Ensure that all the new arrays are unique
function ov4(ar){//var a1=((new Date).getTime()) var m=[],f; for(var i=0;i<ar.length;i++){ f=true; for(var j=0;j<m.length;j++) if(ar[i]===m[j]){f=false;break;}; if(f)m.push(ar[i])}//console.info((new Date).getTime()-a1) return m.sort(function(a,b){return ab});}5. Use object properties
function ov5(ar){// var a1=(new Date).getTime()var m,n=[],o= {};for (var i=0;(m= ar[i])!==undefined;i++)if (!o[m]){n.push(m);o[m]=true;}// console.info((new Date).getTime()-a1) return n.sort(function(a,b){return ab});;}The above recommendations for removing duplicate values in JavaScript arrays are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.