A collection (set) is a set of unordered sets of data that have a certain relevance to each other. Each member can appear only once in the array.
It's a good idea to understand the content before using the set (set):
1. A collection that does not contain any members is called an empty collection.
2, if the members of the two collection are equal, the two sets are equal.
3. If the members in one collection are in another collection, then two have a relationship with the parent-child set.
In the collection we commonly use the operation is: to find the set of the set, intersection, complement set and so on.
Here we build a collection (set) class based on the array.
functionSet () { This. DataStore = []; //adding elements This. Add =function(data) {if( This. Datastore.indexof (data) < 0) { This. Datastore.push (data); return true; } Else { return false; } }; //Delete Element This. remove =function (){ varpos = This. Datastore.indexof (data); if(Pos >-1) { This. Datastore.splice (pos,1); return true; } Else { return false; } }; This. Size =function (){ return This. Datastore.length; }; //ask for a set of two sets This. Union =function(set) {varTempset =NewSet (); for(vari = 0; I < This. datastore.length; ++i) {Tempset.add ( This. Datastore[i]); } for(vari = 0; i < set.dataStore.length; ++i) {if(!tempset.contains (Set.datastore[i])) {TempSet.dataStore.push (set.datastore[i]); } } returnTempset; }; //query Whether the collection contains an element This. contains =function(data{if( This. Datastore.indexof (data) >-1) { return true; } Else { return false; } }; //intersection of two sets This. intersect =function(set) {varTempset =NewSet (); for(vari = 0; I < This. datastore.length; ++i) {if(Set.contains ( This. Datastore[i])) {Tempset.add ( This. Datastore[i]); } } returnTempset; }; //query set is not a subset of the collection now This. subset =function(set) {if( This. Size () >set.size ()) { return false; } Else { foreach (varMemberinch This. DataStore) { if(!Set.contains (member)) { return false; } } } return true; }; //complement of two sets This. Difference =function (){ varTempset =NewSet (); for(vari = 0; I < This. datastore.length; ++i) {if(!set.contains ( This. Datastore[i])) {Tempset.add ( This. Datastore[i]); } } returnTempset; }; This. Show =function (){ return"[" + This. DataStore + "]"; };}</script></body>JavaScript SET Data Structures