Multiple methods for implementing array deduplication in javascript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces a variety of methods for implementing array de-duplication in javascript. If you are interested, you can refer to this article for a little bit of nonsense!

Let's talk about the requirements of this instance: Write a method to implement de-duplication of arrays. (Requirement: Execute the method, pass an array, and return the new array after deduplication. The original array remains unchanged. During implementation, only one layer of loops can be used, and double-layer nested loops can also be written for reference only );

Let's explain to beginners what is array deduplication (old bird skips): this means to remove repeated elements in the array, for example, var arr = [,]; the new array obtained by array deduplication is [3, 2, 4, 1], which is such a function.

The implementation method is relatively simple, and there are many implementation methods. Many Daniel has also written related articles. The reason why I write this blog is that I want to forget it, the second is to give beginners a better understanding of its implementation principles. Well, let's look at the first implementation method:

First, traverse the new array

Var arr = [1, 'B', 'B',]; // The first Array. prototype. unique1 = function () {var arr1 = []; // defines a new array for (var I = 0; I
 
  

If you are not familiar with this writing method, you can change it to the following one, with the same effect:

Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; function unique1 (arr) {var arr1 = []; for (var I = 0; I

I will not rewrite the following method. You can rewrite the method based on the above format, and I will not output the result because the result is the same. Write the comments in the Code and try again.

Second, it is implemented through the hash table (this concept is a bit big, and the specific principle is not described here. I will write it again if I have time. This is a good thing ).

Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; Array. prototype. unique2 = function () {var hash = {}; // defines a hash table var arr1 = []; // defines a new array for (var I = 0; I
    
     


Third, it is implemented by traversing its own locations for consistency

Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; Array. prototype. unique3 = function () {var arr1 = []; // defines a new array for (var I = 0; I
      
       


Fourth, this is a bit interesting. It can only be used in special cases, that is, sorting with arrays first, then comparing with 22, and outputting a new sorted Array

Array. prototype. unique4 = function () {/* here is the idea: first sort (from small to large by default), then the first of the original array to the new array, because it is sorted, therefore, the repeat will only exist in the adjacent position, which is equivalent to doing 22 comparison. If they are equal, the next group will be performed. If they are not equal, the number will be saved to the new array, use this number for comparison */this. sort (); var arr1 = [this [0]; for (var I = 1; I
        
         

Wow, close the job!

The requirement also says that double-layer nested loops can be used for implementation, instead of two-layer for loops, so that each of them can be compared with the original array.

Array. prototype. unique5 = function () {// double-layer loop, one-to-one comparison for (var I = 0; I
          
           

This writing method involves too many cycles and is not recommended. Some people may say that the first and third types are not repeated every time? Is it about the same as 5th? Yes, you can understand it. It means that you understand it, but it is not a special understanding. If we say it is almost the same, it will be much worse, indexOf () it indicates that when the first matching element is found

Stop traversal, and the 5th types will traverse the entire array no matter whether it is not found. If there is a large amount of data, which of the following performance do you think is better?

Note: If the values in the comparison are full or unequal, always use constant (=) and not constant (! =), Because this involves the element type, for example, 1 and '1' are not constant!

The above is really dry, and there is no water at all. You can only understand it by yourself!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.