Using HashSet <int> to Create an Integer Set

I want to create a class representing an integer set using a HashSet<int> . I want it to keep track of which values ​​are included in the set using this inner container. I have done it so far:

 class SetInteger { HashSet<int> intTest= new HashSet<int>(); intTest.Add(1); intTest.Add(2); intTest.Add(3); intTest.Add(4); intTest.Add(5); intTest.Add(6); intTest.Add(7); intTest.Add(8); intTest.Add(9); intTest.Add(10); } 

So, here I think I'm adding some values ​​to the HashSet , but I don’t see how this can track which values ​​are included in the set. Any ideas?

+4
source share
5 answers

The hash set has a Contains method that allows you to check if a value is in the set.

In addition, HashSet<T> implements the ISet<T> interface and, therefore, provides many methods for working with sets, such as combining, intersecting, and determining whether a set of values ​​is (correct) a super- or a subset of your set.

 HashSet<int> intTest = new HashSet<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; bool has4 = intTest.Contains(4); // Returns true bool has11 = intTest.Contains(11); // Returns false bool result = intTest.IsSupersetOf(new []{ 4, 6, 7 }); // Returns true 

By the way, do you know about the initializer syntax of the collection ?


You can also foreach in the set to get each element that it contains (in unspecified order):

 foreach(int value in intTest) { // Do something with value. } 

Or convert it to an array or mutable list (also in unspecified order):

 int[] arr = intTest.ToArray(); List<int> lst = intTest.ToList(); 
+19
source

You can use the HashSet Contains method if the value already exists!

Example:

 if (intTest.Contains(5)) { // already has the value } 
0
source

Hmm ... well, HashSet<T> implements IEnumerable<T> , so you can always do this to find out what’s already there:

 HashSet<int> intTest= new HashSet<int>(); intTest.Add(1); intTest.Add(2); intTest.Add(3); intTest.Add(4); intTest.Add(5); intTest.Add(6); intTest.Add(7); intTest.Add(8); intTest.Add(9); intTest.Add(10); var inThereNow = intTest.ToArray(); // [1,2,3,4,5,6,7,8,9,10] 

There is also a bool Contains(T value) that will tell you if there is a specific value in the set, IEnumerable<T> Union(IEnumerable<T> other) , which will tell you the "OR" of the two sets, IEnumerable<T> Intersect(IEnumerable<T> other) , which will tell you about the overlap of the two sets .. Almost everything in IEnumerable<T> or ISet<T>

0
source

We use the method Contains: http://msdn.microsoft.com/en-us/library/bb356440.aspx

Hope this helps.

-1
source

you can try this. you just take one text block and two buttons.

 HashSet<int> hs = new HashSet<int>(); private void savedataonhashSet_Click(object sender, EventArgs e) { hs.Add(Convert.ToInt16(textBox1.Text)); } private void checkduplicatevalue_Click(object sender, EventArgs e) { if (hs.Contains(00)) { MessageBox.Show("it is"); } else { MessageBox.Show("not there"); } } 

If you run into a problem again, just let go of your code .....

-2
source

All Articles