Teach you the idea and method of array de duplication~

① Methods that do not generate new arrays
Idea: compare an element of the array (named variable x here) with all elements of the element group. If there are duplicates, get the address of the element in the array, and then delete the element!
How do I get the address of this element?
The address of variable x is the address of the duplicate element! For example, a[4] is compared with all elements of array A. if the same element has appeared before a[4], then a[4] is a duplicate element, and its address is 4! Why? Don't forget, they are an array!
  

let a = [1,2,5,7,4,2,1,3,5];
for(let i = 0,len=a.length;i < len;i++){
    let num = 0;
    a.forEach((v,index)=>{
//Put an element in the array (here it's called an element x)Compare with all elements of the array
        if(v==a[i]){
//If the elements are equal, then num++
            num++;
//if num>1,It means that the frequency of an element is more than once, that is, repetition!
            if(num > 1){
//Get events a Position in the array index,Then delete the element in the array
                a.splice(index,1);
            }
        }
    });
}

② Generate new array
Method 1:
Idea: store the array in a new array. If there are existing elements in the new array, they will not be stored. Here you can use the for loop to compare with the elements in the new array, and check whether the new array has elements.

let b = [];    //Create a new array to hold data that does not repeat
for(let i = 0,len = a.length;i < len;i++){
//Judge whether the new array is empty. If it is empty, directly a[0]piece in
    if(!b[0]){
        b.push(a[i]);
    }else{
//Flag to determine whether the element is repeated
    let state = true;
    b.forEach((v,index)=>{
        if(v==a[i]){    
//If repeated, state=false
                state = false;
            }
        });
        if(state==true){
//Ifstate==false,It means that the data is repeated and will not be added to the new array;
//Ifstate==true,That means the data is not repeated
            b.push(a[i]);
        }
    }
}

Method two:
This method is suitable when the element is a number!
Idea: take the value of the element as the key value of the new array. If the keyArr[oldArr[i]] does not exist, attach a value to it and store the arr[i] in the new array newArr. If it exists, then prove that the element oldArr[i] is repeated and not stored in the newArr array!

function oSort(arr){
            var result = [];
             var newArr=[];
            for(var i in arr){
//If there is no element in the arr[i] position in the result array, set the position to 1 and put the arr[i] in the new array
//On the contrary, if there are elements in this position, it is proved that arr[i] is a duplicate element, and it is not necessary to put it in the array
                if(!result[arr[i]]){
                    newArr.push(arr[i]) //Here, you can also replace it with arr.splice(i,1); delete the element so that the child does not need to go to the newArr array                                                                                               
                    result[arr[i]]=1
                }
            }
            return newArr;    //If you use the method of deleting elements, change it to arr accordingly
}

Posted by bertfour on Wed, 01 Apr 2020 10:26:21 -0700