Array to heavy

Methods a

  1. Take each item in the array in turn (Eliminate the last term: There is no comparison after the last item.)
  2. And the current take out itemEach of these termsIn turn,To compare
  3. If we find any duplicates, we’ll take the one we foundduplicatesinThe original numberIn the groupdelete(splice)
  4. i--Preventing array collapse
var arr=[1.2.3.2.3.4.3.4.5]

/ / method
/** *1, select each item in the array in order (exclude the last item: there is nothing to compare after the last item) *2, compare each item in order after the current item *3, if there is a duplicate, we will delete the duplicate item in the array (splice) */

//=>arr. Length -1
for(var i=0; i<arr.length-1; i++){var item =arr[i];       
		// Take each item in turn
		// I: index of the current output item
	// Compare with each item after the current item: the starting index is I +1
	for(var j=i+1; j<arr.length; j++){if(item === arr[j]){
			arr.splice(j,1);
			// This will cause the collapse problem: when we delete the current item, each of the following items will advance one bit, that is, the index of the original array has changed, and then we continue to increment j by one and the next result will skip one bit
			// Add ++ to the listi--; }}}console.log(arr)  / /,2,3,2,4,5 [1]
Copy the code

Method 2: Implement high performance array de-duplication based on non-duplicate attribute names of objects

  1. To create aAn empty object
  2. Iterate over each item in the array, storing the value of each item as the property name and value of the object
  3. Before we store itDetermine if the property name already exists in the current objectIf it exists, it means that there was a previous operation to store this item, which means that the operation is repeatedThe current itemIn the arraydeleteCan be
  4. i--Preventing array collapse
// Method 2: Implement high performance array de-duplication based on non-duplicate attribute names of objects
// create an empty object
//2, iterate over each item in the array, store the value of each item as the attribute name and attribute value of the object
//
// if the property name already exists in the current object before storing it, if it exists, it indicates that the operation of storing this item has been repeated, at this point we can delete the current item in the array
//
// How to determine if the property exists in the object: undefined
//
//
var arr=[1.2.3.2.3.4.3.4.5]
var json={};
for(var i=0; i<arr.length; i++){var item=arr[i];
	//json[item]! = = is undefined
	if(typeofjson[item]! = ='undefined') {/* *arr.splice(i,1) *i--; // Prevent array collapse * * this is not good, each time we delete an item, the following index needs to be recalculated, which is very expensive */
		/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
		arr[i]=arr[arr.length-1];
		arr.length-1;
		i--;
		continue;
	}
	json[item]=item;
}
console.log(arr)
Copy the code

Array.from(new Set(arr))

// ES6 compatibility is not considered
var arr=[1.2.3.2.3.4.3.4.5]
/ / new Set (arr) {1, 2, 3, 4, 5}
var arr2=Array.from(new Set(arr)) 
console.log(arr2) 	/ / [1, 2, 3, 4, 5]
Copy the code

Methods four, arr. Reduce (function (accumulator, currentValue currentIndex, array) {}, the initialValue)

let arr = [1.2.1.2.3.5.4.5.3.4.4.4.4];
[1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 5, 5]
// So just repeat init the last one (' init[init.length-1] ') must be equal to the next one
// init the first loop is [], current the first loop is 1
// init is set to [1] for the second loop, and current is set to 1 for the second loop
// init is set to [1] on the 3rd loop and 2 on the 3rd loop
let result = arr.sort().reduce((init, current) = >{
    if(init.length===0 || init[init.length-1]! ==current){ init.push(current); }returninit; } []);console.log(result); / / [1, 2, 3, 4, 5]
Copy the code