js Array deduplication method

In practice or interviews, we often encounter "array to heavy" issue, the next step is to use a variety of methods to achieve js array of deduplication:


 

1. Set the aid provided by the structural ES6

    var ARR = [1,1,2,2,3,3,4,4,5,5,4,3,2,1,1,1 ]; 
    the console.log (ARR);     // [. 1,. 1 , 2, 2,. 3,. 3,. 4,. 4,. 5,. 5,. 4,. 3, 2,. 1,. 1,. 1] 
    function noRepeat11 (ARR) {
         var newArr = [];
         var myset = new new the Set (ARR); // use the structure of the Set not receive duplicate data characteristics 
        for ( var Val of myset) { 
            newArr.push (Val) 
        } 
        return newArr; 
    } 
    var arr2 is = noRepeat11 (ARR) 
    the console.log (arr2 is);     // [. 1, 2, 3, 4, 5]

2. by the indexOf () method of determining a position index of the first occurrence of this element circulating in the array index is equal

    var ARR = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5 ]; 
    the console.log (ARR);     // [. 1, 23 is ,. 1,. 1,. 1,. 3, 23 is,. 5,. 6,. 7,. 9,. 9,. 8,. 5,. 5,. 5] 
    function noRepeat2 (ARR) {
         for ( var I = 0; I <arr.length; I ++ ) {
             IF (! arr.indexOf (ARR [I]) = I) { 
                arr.splice (I, 1); // delete the array element of the array length minus one element behind the forward 
                i--; // array subscript backoff 
            } 
        } 
        return ARR; 
    } 
    var newArr = noRepeat2 (ARR); 
    the console.log (newArr);     //[1, 23, 3, 5, 6, 7, 9, 8]

3. The method of using the filter array

    var arr = ['apple','banana','pear','apple','orange','orange'];
    console.log(arr)    //["apple", "banana", "pear", "apple", "orange", "orange"]
    var newArr = arr.filter(function(value,index,self){
    return self.indexOf(value) === index;
    });
    console.log(newArr);    //["apple", "banana", "pear", "orange"]

4. With the new array is determined by the current element indexOf square array index if the cycle is equal to the index added to the new array

    was arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5 ]; 
    console.log (ARR)     // [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5] 
    FUNCTION noRepeat4 (ARR) {
         were K = [];
        for ( the i = 0; i <arr.length; i ++ ) {
             if (arr.indexOf (arr [i]) == i) { 
                ret.push (arr [i]); 
            } 
        } 
        Return right; 
    } 
    Was arr2 = noRepeat4 (ARR); 
    console.log (arr2);    // [1, 23, 3, 5, 6, 7, 9, 8]

The use of space to record new objects already stored in the array element through

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr)    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    var obj={};
    var newArr=[];
    for(var i=0;i<arr.length;i++){
        if(!obj[arr[i]]){
            obj[arr[i]]=true;
            newArr.push(arr[i]);
        }
    }
    console.log(newArr);    //[1, 23, 3, 5, 6, 7, 9, 8]

6. With the new array, determines whether or not the new element if there is no element of this array is added to the presence of the new array

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
    function noRepeat6(arr){
        var newArr = [];
        for(var i = 0; i < arr.length; i++){
            if(newArr.indexOf(arr[i]) == -1){
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }
    var arr2 = noRepeat6(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]

7. With the new array, determines whether or not the presence of the element does not exist, if this element is added into a new array (the same length but the original array is sorted in order of character string) in the new array

    var ARR = [1,23,1,1,1,3,23,5,6,7,9,9,8,5 ]; 
    the console.log (ARR);     // [. 1, 23 is,. 1,. 1 ,. 1,. 3, 23 is,. 5,. 6,. 7,. 9,. 9,. 8,. 5] 
    function noRepeat7 (ARR) {
         var RET = [], 
            End; // temporary variables elements repeated for comparison 
        arr.sort (); // the number of re-ordering the group 
        End ARR = [0 ]; 
        ret.push (ARR [ 0 ]);
         for ( var I =. 1; I <arr.length; I ++ ) {
             IF (! ARR [I] = End) { // the current element if the element and varying the temporary add this element to the new array 
                ret.push (ARR [I]); 
                End= Arr [i]; 
            } 
        } 
        Return right; 
    } 
    Was arr2 = noRepeat7 (ARR); 
    console.log (arr2);    // [1, 23, 3, 5, 6, 7, 8, 9]

8. Double cyclically changing the original array

    var arr = [1,1,2,2,3,3,4,4,5,5,4,3,1,2,6,6,6,6];
    console.log(arr);    //[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 1, 2, 6, 6, 6, 6]
    function noRepeat9(arr){
        for (var i = 0; i < arr.length; i++) {
            for (var j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j] && i != j) {//将后面重复的数删掉
                    arr.splice(j, 1);
                }
            }
        }
        return arr;
    }
    var arr2  = noRepeat9(arr);
    console.log(arr2);    //[1, 2, 3, 4, 5, 6]

9. Each element of the array in order to make the comparison with other elements, find duplicate elements, delete

    var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5,5];
    console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5, 5]
    function noRepeat1(arr) {
        for(var i = 0; i < arr.length-1; i++){
            for(var j = i+1; j < arr.length; j++){
                if(arr[i]===arr[j]){
                    arr.splice(j,1);
                    j--;
                }
            }
        }
        return arr;
    }
    var arr2 = noRepeat1(arr);
    console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]

10. With the new array

 

    var ARR = [1,1,2,2,3,3,4,4,5,5,4,3,2,1,1,1 ]; 
    the console.log (ARR);     // [. 1,. 1 , 2, 2,. 3,. 3,. 4,. 4,. 5,. 5,. 4,. 3, 2,. 1,. 1,. 1] 
    var newArr = [];
     for ( var I = 0; I <arr.length; I ++ ) {
         var repArr = []; // receiving data later to be repeated subscripts 
        // inner loop to identify duplicate data subscript 
        for ( var J = I +. 1; J <arr.length; J ++ ) {
             IF (ARR [I] == ARR [J]) { 
                repArr.push (J); // find the index rear duplicated data 
            } 
        } 
        // the console.log (repArr);
        IF (repArr.length == 0) { // If no value was repeated array data is not a duplicate description thereof 
            newArr.push (ARR [I]); 
        } 
    } 
    the console.log (newArr);     // [. 5,. 4,. 3, twenty one]

Guess you like

Origin www.cnblogs.com/yuwenxiang/p/11601395.html