Count of Distinct Int32 Values in .NET

Posted by Eric J. on Stack Overflow See other posts from Stack Overflow or by Eric J.
Published on 2012-06-27T22:07:49Z Indexed on 2012/06/29 3:16 UTC
Read the original article Hit count: 104

Filed under:

I am receiving a stream of unordered Int32 values and need to track the count of distinct values that I receive.

My thought is to add the Int32 values into a HashSet<Int32>. Duplicate entries will simply not be added per the behavior of HashSet.

Do I understand correctly that set membership is based on GetHashCode() and that the hash code of an Int32 is the number itself?

Is there an approach that is either more CPU or more memory efficient?

UPDATE

The data stream is rather large. Simply using Linq to iterate the stream to get the distinct count is not what I'm after, since that would involve iterating the stream a second time.

© Stack Overflow or respective owner

Related posts about .NET