Using HashSet to create an integer set

2019-06-26 20:41发布

问题:

I want to create an class that represents an integer set using a HashSet<int>. I want it to keep track of which values are included in the set using that internal container. I've done this 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 dont see how this can keep track of which values that are included in the set. Any ideas?

回答1:

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

In addition, the HashSet<T> implements the ISet<T> interface and therefore provides many methods for working with sets, such as union, intersection and determining if a set of values is a (proper) super- or 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, did you know about the collection initializer syntax?


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

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

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

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


回答2:

Hmm...well, a HashSet<T> implements IEnumerable<T>, so you can always do this to figure out "What's already in 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's also bool Contains(T value) which will tell you if a specific value is in the set, IEnumerable<T> Union(IEnumerable<T> other) which will tell you the "OR" of two sets, IEnumerable<T> Intersect(IEnumerable<T> other) which will tell you the overlap of two sets...pretty much anything in either IEnumerable<T> or ISet<T>



回答3:

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

Example :

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


回答4:

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

Hope this helps.



回答5:

you can try this. you just take a one textbox and two button.

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 again problem faced just drop your code .....



标签: c# int set