4 ways to remove repeating elements in an array in JS

Source: Internet
Author: User

Working on this problem today, trying multiple methods is unsatisfactory, so write a blog to summarize how to remove duplicate elements in JS.


Method 1:

        array.prototype.method1 = function () {             var arr[];    //defining a temporary array             for (var i = 0; i  < this.length; i++) {    //loops through the current array                  //determines whether the element labeled I in the current array has been saved to a temporary array                  //if it is already saved, skip it, or save this element in a temporary array                  if (Arr1.indexOf (This[i]) &NBSP;==&NBSP;-1) {                     arr.push (This[i]);                 }            }             return arr;         }

    Method 2:

                 Array.prototype.method2 = function () {             var h{};    //Define a hash table              var arr[];  //defines a temporary array                          for (var i  = 0; i < this.length; i++) {    //loops through the current array                  //the element to see if it already exists in the table, Skip if present, otherwise deposit in temporary array                  if (!h[this[i]) {                     //Deposit Hash Table &Nbsp;                    h[this[i]] = true;                     //the current array elements into a temporary array                      arr.push (This[i]);                 }             }             return arr;        }

    Method 3:

        array.prototype.method3 = function () {             //directly define the result array              var arr[this[0]];             for (var i = 1; i < this.length; i++) {     //loops through this array starting from the second item of the array                  //the elements to be judged:                 //if the current element of the array appears in this array for the first time, the position is not i                 //then we can judge that the element i is repeating, otherwise it will be deposited directly into the result array                  if (This.indexof (this[i])  == i) {         &nBsp;           arr.push (This[i]);                 }             }             return arr;                 }

    Method 4:

        array.prototype.method4 = function () {             //to sort the array              this.sort ();             //defining the result array             var arr[this[ 0]];            for (var i = 1;  i < this.length; i++) {    //iterates through the array from the second item of the array                  //determines whether adjacent two elements are equal, or writes an element to an array of results if the data is duplicated for equality                 if (This[i] &NBSP;!==&NBSP;ARR[ARR.LENGTH&NBSP;-&NBSP;1]) {                &nbsP;    arr.push (This[i]);                 }                         }             return arr;                     }

For example:

var arr = [112,112, 34, ' Hello ', 112,112, 34, ' hello ', ' str ', ' str1 ']; Alert (arr.method3 ());

Both Method 1 and Method 3 use the IndexOf () method of the array, which is used primarily to find where the element first appears in the array. Compare waste of resources and time.

Method 2 uses a hash table to write the elements that have already appeared in the subscript form into an object, and the subscript reference is less time-saving than searching with the array indexof () method.

Method 4 is to sort the array first, then compare the values of the adjacent two elements one at a time, using the JS native sort () method.

Readers can write their own program to test the efficiency of the four methods. For example: first, using random function to generate a 50000-length random number array, and then four methods to execute, recording program run time to compare the advantages and disadvantages of four methods.

4 ways to remove repeating elements in an array in JS

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.