Deduplication data in JavaScript


A, indexOf ()

Principle: the original data array, write to the new array, if the new array, without this data, we then write, indexOf () result is -1

. 1      var ARR = [1,2,2,3,5,3,1,5,4,3,4,5,6 ];
 2      // define a new array 
. 3      var arr2 is = setNewArr (ARR);
 . 4      Console .log (arr2 is);
 . 5      // array performs deduplication data 
. 6      function setNewArr (ARR) {
 . 7          // define a new array 
. 8          var newArr = [];
 . 9          // execution cycle 
10          arr.forEach ( function (V) {
 11              // perform character appearance position determination 
12 is              IF (newArr.indexOf (V) == -1 ) {
 13 is                  // If no newArr the data, the data is written into newArr
14                 newArr.push(v);
15             }
16         })
17         return newArr;
18     }

Note: forEach objects can not be used

Second, the double for the cycle

. 1  var arr123 = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5 ];
 2      // package cycle judging function 
. 3      function setNewArr2 (ARR) {
 4          // outer loop, starting from the first cycle to the last 
. 5          for ( var I = 0; I <=. 1-arr.length; I ++ ) {
 . 6              // inner loop from a second number of cycles beginning, to the last cycle 
. 7              for ( var J = I +. 1; J <=. 1-arr.length; J ++ ) {
 . 8                  // determines whether the first and second are not congruent, and if so, the first two delete, move forward while a j 
. 9                  IF (ARR [I] === ARR [j]) {
 10                      arr.splice (j,. 1 );
 . 11                     J, ;
 12 is                  }
 13 is              }
 14          }
 15          return ARR;
 16      }
 . 17      // reference variable of the function 
18 is      var newArr = setNewArr2 (arr123);
 . 19      the console.log (newArr);

Note:

Array operating cycle, whenever the cell array delete operation

Be sure to perform loop variable value - (local and reduction) operation

Third, the first sort, after de-emphasis
concepts: values in the first array, the order, delete duplicates comparison

. 1              var ARR = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5 ];
 2          // define a new array 
. 3          var newArr = setNewArr3 (ARR );
 . 4          the console.log (newArr);
 . 5          // package a function 
. 6          function setNewArr3 (ARR) {
 . 7              // first operation to sort the array, the same data adjacent 
. 8              var = arr.sort (newArr function (a, B) { return A- B});
 . 9              the console.log (newArr);
 10              // adjacent arrays are aligned 
. 11              for ( var I = 0; I <= arr.length -. 1; I ++ ) {
12                  // if the adjacent array of the same, the following data is deleted, compared with the next data bit complement 
13 is                  IF (newArr [I] === newArr [I +. 1 ]) {
 14                      newArr.splice (I +. 1 ,. 1 )
 15                      i-- ;
 16                  }
 . 17              }
 18 is              return newArr;
 . 19          }

Note:

First array values, in order of size, are arranged
after the sorting, the same values must be adjacent
this case, if two adjacent numerical comparison, if the same, a data delete, i-- then performed
using one can cycle the
number of cycles is relatively small

Guess you like

Origin www.cnblogs.com/karl-kidd/p/12555990.html