Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ JavaScript subarray Deduplication     - shell script error dirname: invalid option - b (Database)

- MongoDB Study Notes (1) - Install MongoDB on Windows systems (Database)

- Oracle11g Trigger Debugging Record Error: PLS-00201: identifier SYS.DBMS_SYSTEM 'must be declared (Database)

- Cross server / client backup command: rsync use (Server)

- Linux suffered SYN flood attack setting (Linux)

- Linux vi command list (Linux)

- Oracle ORA-01089 failure analysis (Database)

- JavaScript: understanding regular expressions (Programming)

- DBCA Error: ORA-19809: limit exceeded for recovery files process (Database)

- Radius server setup under CentOS (Server)

- Shilpa Nair interview experience sharing RedHat Linux package management (Linux)

- Linux rpm command Detailed (Linux)

- To compile and install Clang 3.5.0 in Linux CentOS (Linux)

- GNU / Linux system, how to clean up memory space (Linux)

- Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)

- Configuring s3c-linux-2.6.28.6-Real6410 appears Unable to find the QT3 installation (Linux)

- Use OpenSSL to generate a certificate (Linux)

- How to manage start-up applications in Ubuntu (Linux)

- Ubuntu server 8.04 Firewall Guide (Linux)

- The bulk batch multiple rows of data collect (Database)

 
         
  JavaScript subarray Deduplication
     
  Add Date : 2016-07-25      
         
         
         
  Preface: Yesterday saw the others made the post, talked about the array face questions that often appear to weight problems. As a love of learning, like to listen to the teacher, then a good child paper, ears suddenly remembered inculcate high school teacher: Do not get a look at the answers to questions, we must first think for themselves to answer, and then answer the control test. So I began to think of the process:

First, I think the result is to create another array, the array is used to store the original data will not be repeated. Successively traverse the original array elements in an array with the results of comparing detect duplicate. Ever since, I wrote the following code A:

Array.prototype.clearRepetitionA = function () {
    var result = [];
    var isRepetition;
    for (var i = 0; i         isRepetition = false;
        for (var j = 0; j             if (this [i] === result [j]) {
                isRepetition = true;
                break;
            }
        }
        if (! isRepetition) {
            result.push (this [i]);
        }
    }
    return result;
}

After writing, I suddenly remembered a few days ago just to see ECMAScript 5 arrays indexOf method can retrieve the array elements. So I use the indexOf method instead of the second loop, write the following code B:

Array.prototype.clearRepetitionB = function () {
    var result = [];
    for (var i = 0; i         if (result.indexOf (this [i]) == -1) {
            result.push (this [i]);
        }
    }
    return result;
}

Kazakhstan, once the code from line 17 into a 9-line, and more concise. Middle School Mathematics Solution of large problems usually more than one, ah, then I will continue to think of other ways. Meaning indexOf method is to search for a given value of elements in the array has a whole, the index returns the first element found and not found it returns -1, the first parameter is the value to search for, and the second parameter is optional: it specifies the array index, from where to start the search, if you omit this parameter, the search from scratch. A divergent thinking, thought to detect whether the value of the previous methods are repeated, there is now no indexOf method can be compared to determine the index is equal to the first element of each of the detected first occurrence of the element itself and the index value Well if repeated. So, I wrote the code C:

Array.prototype.clearRepetitionC = function () {
    var result = [this [0]];
    for (var i = 1; i         if (this.indexOf (this [i]) == i) {
            result.push (this [i]);
        }
    }
    return result;
}

Finished this, and continue thought, really can not think other way (hey, these three methods are very basic methods, data structures, algorithms did not learn, really can not think of what shaking, ZZZZZZZZZ Niubi method). So, I went to answer the control, inspection himself. A look at the answer, found himself too weak or real simple problem or some whimsy. The following is not they want to, and do not say too much of my mentality. Ado, directly on the classic answer + resolved.

First, let's say an algorithm is often said that the solution space for time, to remain in line, we call it D bar codes:

 Array.prototype.clearRepetitionD = function () {
    var result = [];
    var obj = {};
    var key, type;
    for (var i = 0; i         key = this [i];
        type = typeof key;
        if (! obj [key]) {
            obj [key] = [type];
            result.push (key);
        } Else if (obj [key] .indexOf (type)) {
            obj [key] .push (type);
            result.push (key);
        }
    }
    return result;
}

This method to save the original value of elements in the array with the properties of an object obj when traversing the original array. At the same time the value of this attribute is an array to store this type of property, which can be similar to the original array element number 1, and the string of '1' to distinguish elements. This method of constructing an object by an additional way to reduce the time indexOf method takes three methods above, we can say it is more efficient.

If you are already satisfied with the above-mentioned method of space for time efficient and not to read on, then it would be wrong, always behind the play thing. Now play the opening, there is no doubt that the E Code:

Array.prototype.clearRepetitionE = function () {
    var result = [];
    for (var i = 0; i         for (var j = i + 1; j             if (this [i] === this [j]) {
                j = ++ i;
            }
        }
        result.push (this [i]);
    }
    return result;
}

Code D space for time, it feels like a general. Then the code E do? I rub this bar code is wrong, this is really important to be able to do? Yes, at first I did not understand this code, and then read it again after reading resolve come to understand. Well, did not understand Tell me what should be a serious look at resolved: the first layer of the original array traversal from front to back, second loop element is to detect whether each element after repeated saying, if there are duplicate elements after it is skip it; if after all the elements related to this element he does not repeat, and put it into the result array. This method is the realization of ideas: Get no repeat of the rightmost value is added to the result array, compared with the first method, the second layer is also optimized cycle efficiency than it is high, but the results of this method array order of the elements with the original order of elements in the array is not the same.

E parsing code reading you are not already out of the thumb, put out the eyes admire it? (These do not give me flowers and honor, should give this method to write to the great god). Besides following the last method: that is the first sort, go heavy. The old rules, it is called Code F:

Array.prototype.clearRepetitionF = function () {
    this.sort ();
    var result = [this [0]];
    for (var i = 1; i         if (this [i]! == result [result.length-1]) {
            result.push (this [i]);
        }
    }
    return result;
}

The first method used to sort the array sort array elements were sorted before proceeding to re-work. This efficiency is really high? Hey, what did not learn arithmetic, and I will answer three words: I do not know. If you know tell me welcome the comments area.
     
         
         
         
  More:      
 
- Oracle 11g How dataguard master repository to Oracle single instance data recovery (Database)
- MySQL binlog automatic cleanup script (Database)
- Docker command Detailed (Linux)
- How do I upgrade to Ubuntu 15.04 (Beta) (Linux)
- Oracle 12C truncate table cascade (Database)
- Installation and deployment of MariaDB under CentOS (Database)
- Use install_updates upgrade GAMIT / GLOBK (Linux)
- Linux installation beautify early experience (Linux)
- Java garbage collection (Programming)
- How to compare PDF files on Ubuntu (Linux)
- Use GLOBK batch command network adjustment (Linux)
- Learning UNIX good habits (Linux)
- JITwatch installation under Linux (Linux)
- Difference between TCP and UDP protocols (Linux)
- OpenSIPS offline messaging feature set (Server)
- Java reflection mechanism explained in detail and Method.invoke explanation (Programming)
- Linux using DenyHosts prevents ssh cracks (Linux)
- Linux platform NTOP Installation and Configuration (Linux)
- MySQL uses mysqld_multi to deploy stand-alone multi-instance detail procedures (Database)
- TeamCity continuous integration in the Linux installation (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.