Reputation: 41
I have a problem with HashSet in C#....
this is my code:
List<int> elements = new List<int>();
for(int i = 0;i< 100000;i++)
{
elements.Add(i);
}
HashSet<int> c = new HashSet<int>();
foreach(var ele in elements)
{
c.Add(ele);
}
Console.WriteLine("Working HashSet " + c.Count);
var Numbers = new HashSet<int>();
var mycount = 0;
using (TextReader reader = File.OpenText(@"myfile.txt"))
{
while ((line = reader.ReadLine()) != null)
{
mycount++;
int parsed = int.Parse(line);
Numbers.Add(parsed);
}
}
Console.WriteLine("my counter took" + mycount);
Console.WriteLine("Bad HashSet" + Numbers.Count);
Working HashSet 100 000
my counter took 500 000
Bad HashSet 9999
why the second hashset is not adding 500 000 items???? this is a mistery to me
Upvotes: 0
Views: 213
Reputation: 113242
Presumably there're some duplicates. HashSet<T>
represents a set, and provides set operations, hence the name. This is what we use it for.
Upvotes: 0
Reputation: 14678
A HashSet
will not add duplicate numbers, because that's the way sets work.
For example, let's say these are the first few lines of myfile.txt
:
1
2
3
1
2
3
4
You will be iterating over 7 values, but there are only 4 unique numbers in those lines, and HashSet will not add duplicates of 1, 2, or 3. In your case, you have 500,000 lines but only 9,999 unique numbers.
Upvotes: 5