配列の重さを取り除く10の方法

4179 ワード

    var arr1 = [56, 69, 66, 32, 56, 89];
    var arr2 = oSort(arr1);
    console.log(arr1);
    console.log(arr2);


    //    、    
    function oSort(arr) {
      for (var i = 0; i < arr.length; i++) {
        var item = arr[i]
        for (var j = i + 1; j < arr.length; j++) {
          if (item == arr[j]) {
            arr.splice(j, 1);
            j--;
          }
        }
      }
      return arr;
    }



    //    、                    
    function oSort(arr) {
      var o = {};
      var new_arr = [];
      for (var i = 0; i < arr.length; i++) {
        var k = arr[i];
        if (!o[k]) {
          new_arr.push(k);
          o[k] = 1;
        }
      }
      return new_arr;
    }


    //    、  indexOf()                               
    function oSort(arr) {
      for (var i = 0; i < arr.length; i++) {
        if (arr.indexOf(arr[i]) != i) {
          arr.splice(i, 1);//                  
          i--;//      
        }
      }
      return arr;
    }


    //    、      filter  
    var r = arr1.filter(function (element, index, self) {
      return self.indexOf(element) === index;
    });
    console.log(r);


    //    、        indexOf                                         
       
    function oSort(arr) {
      var ret = [];
      for (var i = 0; i < arr.length; i++) {
        if (arr.indexOf(arr[i]) == i) {
          ret.push(arr[i]);
        }
      }
      return ret;
    }


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


    //    (                 )                             
                 
    var arr = [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5];
    function removeRepEle(ar) {
      var ret = [],
        end;//            
      ar.sort();//       
      end = ar[0];
      ret.push(ar[0]);
      for (var i = 1; i < ar.length; i++) {
        if (ar[i] != end) {//                         
          ret.push(ar[i]);
          end = ar[i];
        }
      }
      return ret;
    }

    arr2 = removeRepEle(arr);
    console.log(arr);//[ 1, 1, 1, 1, 23, 23, 3, 5, 5, 6, 7, 8, 9, 9 ]
    console.log(arr2);//[ 1, 23, 3, 5, 6, 7, 8, 9 ]


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

    //    (         )
    var arr = [1, 1, 1, 3, 4, 4, 4, 5, 5, 5, 5, 4, 6];

    function removeArrayRepElement(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 = removeArrayRepElement(arr);
    console.log(arr); //[ 1, 3, 4, 5, 6 ]
    console.log(arr2);//[ 1, 3, 4, 5, 6 ]

    //   (     )
    var arr = [12, 2, 44, 3, 2, 32, 33, -2, 45, 33, 32, 3, 12];
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
      var repArr = [];//           
      //              
      for (var j = i + 1; j < arr.length; j++) {
        if (arr[i] == arr[j]) {
          repArr.push(j);//           
        }
      }
      //console.log(repArr);
      if (repArr.length == 0) {//                 
        newArr.push(arr[i]);
      }
    }
    console.log(newArr);//[ 44, 2, -2, 45, 33, 32, 3, 12 ]