Summary of three common methods of js array deduplication

Source: Internet
Author: User

This article mainly summarizes the three common methods for removing js array duplicates. If you need some help, please refer to them.

The first method is more conventional: 1. create a new array to store result 2. in the for loop, an element is extracted from the original array each time, and compared with the result array through this element loop. 3. if this element is not in the result Array, the code in the result Array is as follows: Array. prototype. unique1 = function () {var res = [this [0]; for (var I = 1; I <this. length; I ++) {var repeat = false; for (var j = 0; j <res. length; j ++) {if (this [I] = res [j]) {repeat = true; break ;}} if (! Repeat) {res. push (this [I]) ;}} return res;} var arr = [1, 'A', 'A', 'B', 'D', 'E ', 'E', 1, 0] alert (arr. unique1 (); the second method is more efficient than the above method. sort the original array first. check whether the I-th element in the original array is the same as the last element in the result array. Because the elements have been sorted, the repeated elements are in the adjacent position. 3. if they are different, the code for saving the element to the result Array is as follows: Array. prototype. unique2 = function () {this. sort (); // first sort var res = [this [0]; for (var I = 1; I <this. length; I ++) {if (this [I]! = Res [res. length-1]) {res. push (this [I]) ;}} return res;} var arr = [1, 'A', 'A', 'B', 'D', 'E ', 'E', 1, 0] alert (arr. unique2 (); the second method also has some limitations. Because sorting is performed before de-duplicating, the de-duplicating results returned at the end are also sorted. If you do not need to change the order of the array to deduplicate, this method will not be available. Method 3 (recommended) Ideas: 1. create a new array to store result 2. create an empty object 3. during a for loop, each time an element is retrieved and compared with an object, if the element is not repeated, it is stored in the result array, and the content of this element is used as an attribute of the object, and assign a value of 1 to the object created in step 1. Note: For comparison, an element is extracted from the original array each time and accessed in the object. If the attribute can be accessed, it is repeated. The copy code is as follows: Array. prototype. unique3 = function () {var res = []; var json ={}; for (var I = 0; I <this. length; I ++) {if (! Json [this [I]) {res. push (this [I]); json [this [I] = 1 ;}} return res;} var arr = [112,112, 34, 'Hello, 112,112, 34, 'Hi ', 'str', 'str1']; alert (arr. unique3 ());

Related Article

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.