Using JS to implement those data structures 10 (collection 02-set operation)

Source: Internet
Author: User
Tags new set

In the previous article we implemented the custom set collection class together. So, let's add some action methods to the set class. Before you begin, it is necessary to explain what the collection is doing. Allows us to understand the code more quickly.

1, and set: For a given set of two, returns a new collection that contains all the elements of the two collection. Note that there are no duplicate values in the collection.

2. Intersection: For a given set of two, returns a new collection containing two elements of the set.

3, Difference set: For a given collection, returns a new collection that contains all the elements that exist in the first collection and do not exist in the second collection. In short, I have elements that you don't have.

4. Verifies whether a given collection is a subset of another collection.

Here we don't go into detail again the mathematical calculation method of the set operation. Interested or forget the small partners can Baidu a bit. So let's formally start the collection operation method.

First, and the collection

The this.union = function (otherset) {    //stores a new collection of two collection elements, which we will return as return values. Let    unionset = new Set ();    Values is the array list for the current set let's    values = this. values ();    Loop join for    (let i = 0; i < values.length; i++) {        unionset.add (values[i]);    }    Re-copy values    values = otherset.values ();    By looping the value of Otherset into Unionset, because our add does not add a duplicate value, naturally there is no duplicate value in Unionset for    (Let i = 0; i < values.length; i++) { Unionset.add (Values[i]); } return Unionset;}      

We found that, in fact, the operation of the set is very simple, is to declare a new set, and then by looping two seta and SETB values in the new Unionset. It's not complicated.

Let SetA = new set (), Seta.add (1), Seta.add (2); Seta.add (3); let Setb = new set (); Setb.add (3); Setb.add (4); Setb.add (5); Setb.add (6); let Unionab = seta.union (SETB); Console.log (Unionab.values ()) ;//[1, 2, 3, 4, 5, 6]         

Second, intersection

Intersection Operation This.intersection = function (otherset) {let  intersectionset = new Set ();  Let values = this. values ();  for (Let i = 0; i < values.length; i++) {      if(Otherset.has (Values[i])) {          Intersectionset.add ( Values[i]      }}  return intersectionset;}     

The intersection operation is actually very simple, a word is to check whether the elements in the Seta are also in the SETB, if there is so long deposit in Intersectionset. It's just as if we were looking for the same element in two arrays.

Let setc = new set (), Setc.add (5), Setc.add (6); Setc.add (7); let setd = new set (); Setd.add (5); Setd.add (7); Setd.add (4); Setd.add (8); let Intersectionsetcd = setc.intersection (setd); Console.log ( Intersectionsetcd.values ());//[5,7]         

Three, the difference set

Difference Set Operation this.difference = function (otherset) {let  differenceset = new Set ();  Let values = this. values ();  for (Let i = 0; i < values.length; i++) {      //is only more than the intersection operation here's judgment changed to non (! ) only      if (!  Otherset.has (Values[i]) {          differenceset.add (values[i])      }  }  return differenceset;  

The operation of the difference set and the operation of the set are very similar. The combination is an element that needs to exist in two collections ( you have me too ), and the difference is present in Seta but does not exist in Setb (you have me not).

So we just need to change the intersection code a little bit.

Let Setm = new set (), Setm.add (5), Setm.add (6); Setm.add (7); let Setn = new set (); Setn.add (5); Setn.add (7); Setn.add (4); Setn.add (8); let differencesetmn = setm.difference (SETN); Console.log ( Differencesetmn.values ());//[6]         

Iv. subsets

Subset Operation This.subset = function (otherset) {    otherset.size ()) {        return false;    } else { Let        values = this. values ();        for (Let i = 0; i < values.length; i++) {if (!  Otherset.has (Values[i])) {return false;}} return True;}}        

In fact, the subset operation is not a good explanation, just to note that if the subset of SETA is SETB, then the number of SETA elements is necessarily greater than or equal to SETB. Otherwise SETB could not be a subset of the SETA. So we start by judging whether the size of the element conforms to this definition.

Well, if it does, we're traversing the entire SETB element, judging whether it exists in the SETA, returning False if there is a non-existent, and returning true if the traversal end is present.

Let SetX = new set (), Setx.add (1), Setx.add (2), let sety= new set (); Sety.add (1); Sety.add (2); Sety.add (3); let setz= new Set (); Setz.add (2); Setz.add (3); Setz.add (4); Console.log (Setx.subset ( sety));//trueconsole.log (Setx.subset (Setz)           );//false

Here we introduce the operation of the collection, is not very simple. So let's take a look at what the ES6 native set class looks like.

ES6 Native Set class

  Let's take a look at a simple example of the native set class:

Let set = new set (), Set.add (1), Console.log (Set.values ()),//setiterator {1}set.add (2); Console.log ( Set.has (1));//trueconsole.log (set.size)//2console.log (Set.delete (1));//trueconsole.log (set.size)//1console.log (Set.has (1));//falseconsole.log (Set.has (2));//true  

The native set class has methods such as has (), add (), delete (), clear (), and so on. Also has a traverse method such as values (), keys (), entries (), ForEach (), and a size property. There is no detailed introduction to each of the attribute methods, want to learn more in-depth you can check it yourself.

So let's see how to manipulate the collection with the native set class.

Let SetA = newSet (); Seta.add (1); Seta.add (2); Seta.add (3); Let Setb = newSet (); Setb.add (2); Setb.add (3); Setb.add (4);//analog and set operation let Unionab = newSet ();//actually the same principle as our custom and set operation, traversing two sets separately and adding its elements to Unionab//for...of This operation is also the ES6 loop traversal method. For (Let X of SetA) Unionab.add (x), for  (Let X of Setb) Unionab.add (x); Console.log (Unionab.values ())// Setiterator {1, 2, 3, 4}//Analog intersection operation//simulation intersection operations need to create a helper function to generate a new collection that contains both Seta and SETB elements. Let intersetion = function  (SETA,SETB) {Let intersetionset = new  Set (), for  (Let X of SetA) {if  (s Etb.has (x)) {intersetionset.add (x);}} return  Intersetionset;} Let Intersetionab =  intersetion (SETA,SETB); Console.log (Intersetionab.values ())//setiterator {2, 3}// Analog differential set operation//the same, with the intersection operation is very similar, but the condition is just the opposite of let difference = function  (SETA,SETB) {Let differenceset = new  Set (); for< span> (Let X of SetA) {if (! ) Setb.has (x)) {differenceset.add (x);}} return  Differenceset;} Let Differenceab =  difference (SETA,SETB); Console.log (Differenceab.values ())//setiterator {1}  

When we finished writing the ES6 native set class simulation set operation, we found that it was very similar to our custom set operation method. It's just that we used the ES6 native interface. You can try to compare these two types of code. Deepen your impressions.

The collection here is complete. Looking back at the code, we found that the various methods of operation of the collection are often applied in our actual work, but we do not pay much attention to these details when we are working with arrays. For example, the union operation, we have used when merging two arrays. For example, the intersection operation, we will use when we look for the common elements in two arrays. So in fact, we have used or often used the idea of collection operation in our work.

  

Finally, because my level is limited, the ability and the great God is still very far apart, if there are errors or unclear, but also hope that everyone is not hesitate to correct. Thank you so much!

Using JS to implement those data structures 10 (collection 02-set operation)

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.