Several implementation methods of array de-weight

Source: Internet
Author: User
Tags dedupe

1. Use Set

ES6 provides a new data structure set, which resembles an array, like a set container in C + +, whose member values are unique and have no duplicate values; The set itself is a constructor that is used to generate a set data structure.

var New Set (); S.add ("Hello"). Add ("Goodbye"). Add ("Hello"= = = 2; S.has (true;
New Set (); [2,3,5,4,5,2,2].foreach (x = s.add (x));  for (Let I of s) {    console.log (i);} // 2 3 5 4

There's a simpler way.

function Dedupe (array) {    return array.from (new  Set (array));} Dedupe ([1, 1, 2, 3]); // [1, 2, 3]

The above approach is that the Array.from method can convert the set structure to an array. If you still don't feel like it, there is another way to express it: Extend the operator (...) Combined with the set structure, the duplicate members of the array can be removed, and the For...of loop is used internally by the extension operator.

[... New Set (Array)]

2. Methods of IndexOf

This method creates a new storage array, iterates over the parameter array, and if the index of the element is not found in the new array, push the element into the new array.

function Dedupe (array) {    var newArr = [];      for (var i=0; i<array.length; i++) {        if(Newarr.indexof (array[i]) = =-1) {            Newarr.push (Array[i]);        }    }     return NEWARR;} var arr = [1, 1, 2, 2, 3, 3];console.log (Dedupe (arr)); // [1, 2, 3]

3. Comparing array subscripts

We know that by comparing array subscripts to find duplicate numbers from an array, it is natural to judge by the index value I at the first time of the array member, which is similar to the IndexOf method.

function Dedupe (array) {    var newArr = [];      for (var i=0; i<array.length; i++) {        if(Array.indexof (array[i]) = = i) {            Newarr.push (Array[i]);        }    }     return NEWARR;} var arr = [1, 1, 2, 2, 3, 3];console.log (Dedupe (arr)); // [1, 2, 3]

4. Object Lookup

functionDedupe (arr) {varres = []; varo = Object.create (NULL);  for(let V of arr) {varType =typeofv; if(!O[v])            {Res.push (v); O[V]=[Type]; }Else if(O[v].indexof (type) = =-1) {O[v].push (type);        Res.push (v); }    }    returnRes;}vararr = [1, 2, ' 2 ', 4, 7, ' A ', ' a ', 2, 3, 7, 6, 7];console.log (Dedupe (arr));//[1, 2, ' 2 ', 4, 7, ' A ', 3, 6]

5. Sort adjacent to weight

function Dedupe (arr) {    arr.sort ();     var newArr = [arr[0]];      for (var i=1; i<arr.length; i++) {        if(Arr[i]!== newarr[newarr.length-1]) {             Newarr.push (Arr[i]);        }    }     return NEWARR;} var arr = [1, 2, ' 2 ', 4, 7, ' A ', ' a ', 2, 3, 7, 6, 7];console.log (Dedupe (arr)); // [1, "2", 2, 3, 4, 6, 7, "a"]

6. Includes method for array instances

function Dedupe (arr) {    var res=[];      for (let V of arr) {        if(!  Res.includes (v)) {            Res.push (v);        }    } return Res;} var arr = [1, 2, ' 2 ', 4, 7, ' A ', ' a ', 2, 3, 7, 6, 7];console.log (Dedupe (arr)); // [1, 2, "2", 4, 7, "a", 3, 6]

7. Violence to regain

functionDedupe (arr) {arr.sort ();    Console.log (arr);  for(varI=1; i<arr.length; i++) {        if(arr[i]===arr[i-1]) {Arr.splice (I,1); I--; }    }    returnarr;}vararr = [1, 2, ' 2 ', 4, 2, 7, ' A ', ' a ', 2, 3, 7, 6, 7, 7, 7];console.log (Dedupe (arr));//[1, "2", 2, 2, 2, 3, 4, 6, 7, 7, 7, 7, 7, "a", "a"]//[1, "2", 2, 3, 4, 6, 7, "a"]varArr0 = [5, 1, 2, ' 2 ', 4, ' 2 ', 2, 5, 7, ' A ', ' a ', 2, 3, ' 7 ', 6, ' 7 ', 7, 7];console.log (Dedupe (arr));//[1, 2, "2", "2", 2, 2, 3, 4, 5, 5, 6, 7, 7, "7", 7, "7", "A", "a"]//[1, 2, "2", 2, 3, 4, 5, 6, 7, "7", 7, "7", "a"]

In this way, it is best to use only in a numeric array or a string array to weight, due to the sort () Order of the reason, plus only compare adjacent elements are equal, when the occurrence of similar arrays such as ARR0, the de-weight will be omitted; therefore, it is better to go to the single type array;

Reference:

http://es6.ruanyifeng.com/#docs/set-map

http://es6.ruanyifeng.com/#docs/array

Several implementation methods of array de-weight

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.