Array to weight / letter string deduplication

1. deduplication array 

The basic idea indexOf ():
  create a new array, the original array value is written to the new array. If this value does not exist in the new array on the implementation of written, if there is not already written.

      // repeat the values stored in the array, after removal of duplicate values, it should be [1,2,3,4,5] 
        var ARR = [1,1,1,1,1,2,2,2,2,2 , 2,3,3,3,3,3,3,4,4,4,4,5,5,5,5,5] 
// create an empty array to the storage array does not duplicate the original data var = newArr []; // loop through, all values acquired in the original array arr arr.forEach (function (V) {
// new array, to find the value of the currently acquired original array newArr.indexOf (V)
       // If the execution result is -1, demonstrated in a new array, the array is not the data of the original IF (newArr.indexOf (V) === -1) { // this data will be written into the new array newArr.push ( V) } }) the console.log (newArr);

  

2. letter string deduplication 
  principles and the same weight to an array, but can not use the string forEach () loop.

  Establishing a new string, the value in the original string is written to a new string. If this value does not exist in the new writing is performed on the string, if not already written.

      STR = var 'aaabbbbcccddddeeeeefffff'; 

        // create a new string, the string is stored in the original data do not overlap 
        var newStr = ''; 

        // by for ... in to loop through the string (not used forEach ) 
        // Key is stored for each index letter subscript 
        for (var Key in STR) { 
            // STR [Key] is the index of the current cycle index acquired, the corresponding letter string 
            // newStr.indexOf ( str [key]) determined the new string, this letter if there is a current 
            // is -1 if the result does not prove that the new string in the letter 
            IF (newStr.indexOf (str [key]) === -1) { 
                / / string splicing operation performed, the letter will be spliced into the new string 
                newStr + STR = [Key]; 
            } 
        } 
        the console.log (newStr);

  ----- a white front against the war from the one thousand classes

Guess you like

Origin www.cnblogs.com/hwy6/p/12549774.html