This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Scheme 1: array double loop comparison
  • Take each item of the array in turn (excluding the last item: there is nothing to compare later)
  • Compare with each item after the current take out item
  • If a duplicate is found, delete the duplicate item from the original array (splice).

This approach is particularly wasteful of performance

var ary=[1.2.1.5.4.3.2.6];
for(var i=0; i<ary.length-1; i++){var item = ary[i]
	// item: Take out each item in turn
	// I: index of the current item
	// Compare with the last item of the current item: the starting index should be I +1, k
		for(var k = i+1; k<ary.length; k++){// ary[k]: the value that needs to be compared with the current item
		if(item==ary[k]){
		// Equality, i.e. duplicates, is removed from the original array
			ary.splice(k,1)
			// This leads to the problem of array collapse: the length of the array changes when the duplicates are deleted, and k accumulates, so that the result jumps back one bit
			k--;// Delete the first subtraction, then add, equivalent to no subtraction}}}console.log(ary);  //[1, 2, 5, 4, 3, 6]
Copy the code
Scheme 2: Object key-value pair based deduplication

Object – based attribute names cannot be repeated to achieve high – performance array deduplication

  • Creating an empty object
  • Iterate over each item in the array, storing each value as the property name and value of the object
  • Check whether the same attribute name already exists in the object before storage (whether duplicate), remove the current duplicate item from the array
var ary=[1.2.1.5.4.3.2.6],
	    obj = {}
    for(var i=0; i<ary.length; i++){var item = ary[i]
		// item: Take out each item in turn
		// I: index of the current item
		// Check whether the attribute name already exists before storing
		if(typeof obj[item]=='undefined') {Attribute name = attribute value
			obj[item] = item
		}else{
			// Delete method 1
			ary.splice(i,1);// The index must be recalculated after deletion
			i--;
			
			// Delete method 2
			//1. Replace the current duplicate item with the last one
			//2. Delete the last item, the length is still reduced by 1
			//3. I -- continues to match the current index entry
			ary[i] = ary[ary.length-1]; ary.length--; i--; }}console.log(ary);  //[1, 2, 5, 4, 3, 6]
Copy the code
Scheme 3: Use set method to remove weight

If compatibility issues are not considered, the es6 syntax, set(), can be used to implement de-duplication

	var ary=[1.2.1.5.4.3.2.6],
		newAry=[];
		newAry = Array.from(new Set(ary))
		console.log(newAry)// [1, 2, 5, 4, 3, 6]
Copy the code
Scheme 4: Encapsulate the array deduplication method
	var ary=[1.2.1.5.4.3.2.6].function unique(ary){
		var obj={};
		In /hasOwnProperty/ in/hasOwnProperty/ in/hasOwnProperty/ in/hasOwnProperty/ If not, continue to store
		for(var i = 0; i <ary.length; i++){var item = ary[i];
            if(obj.hasOwnProperty(item)){
                ary.splice(i,1); / / method
                /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                
                / / method 2
                ary[i] = ary[ary.length-1];
                ary.pop();
                
                i--;
                continue;
            }
            obj[item] = item;
        }
        obj=null;  //obj ();
        return ary;
	}
Copy the code
Scheme 5: a prototype extension based on the built-in class for instance fetching
var ary=[1.2.1.5.4.3.2.6]
// Attribute names can be prefixed to prevent attribute name conflicts
Array.prototype.myUnique = function myUnique(){
	// This in a method is usually an instance of the current class (the array we are manipulating)
	// This is equivalent to ary, which changes the array
	var obj={};
	for(var i = 0; i <this.length; i++){var item = this[i];
        obj.hasOwnProperty(item) ? (this[i] = this[this.length - 1].this.length--,i--) : obj[item] =item;
        obj = null;
        return this; }}var max = ary.myUnique().ary.sort(function(a,b){
	return a- b;
}).pop();
Copy the code
Sort an array
var ary=[1.2.1.5.4.3.2.6]
//sort is a built-in array.prototype property, and ary is an instance that can find methods on the prototype based on _proto_, then d
ary.sort(function(a,b){
	return a- b;
}).reverse().slice(2.7).join('+').split('+').pop()// It is not an array
The result of each method execution is still an instance of the current class, so that the callback method can continue to use
Copy the code