Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define: What is a HashSet?

Tags:

c#

hashset

HashSet The C# HashSet data structure was introduced in the .NET Framework 3.5. A full list of the implemented members can be found at the HashSet MSDN page.

  1. Where is it used?
  2. Why would you want to use it?
like image 777
001 Avatar asked Dec 29 '10 23:12

001


People also ask

Why is it called a HashSet?

A HashSet is called a HashSet because hashing is indeed important to its functionality. Operations like contains(Object) (arguably the most important method in a Set ) and remove(Object) are able to work in constant time by making use of the hash code of the object (by way of HashMap ).

How is a HashSet defined in C++?

Definition of C++ hashset. Hashset can be defined as an unordered collection that consists of unique elements. Hashset consists of standard operation collection such as Contains, Remove, Add; it also constitutes of the standard set-based operations like symmetric difference, intersection, and union.

What is a HashSet data structure?

The underlying data structure for HashSet is Hashtable. As it implements the Set Interface, duplicate values are not allowed. Objects that you insert in HashSet are not guaranteed to be inserted in the same order. Objects are inserted based on their hash code. NULL elements are allowed in HashSet.


1 Answers

    1. A HashSet holds a set of objects, but in a way that it allows you to easily and quickly determine whether an object is already in the set or not. It does so by internally managing an array and storing the object using an index which is calculated from the hashcode of the object. Take a look here

    2. HashSet is an unordered collection containing unique elements. It has the standard collection operations Add, Remove, Contains, but since it uses a hash-based implementation, these operations are O(1). (As opposed to List for example, which is O(n) for Contains and Remove.) HashSet also provides standard set operations such as union, intersection, and symmetric difference. Take a look here

  1. There are different implementations of Sets. Some make insertion and lookup operations super fast by hashing elements. However, that means that the order in which the elements were added is lost. Other implementations preserve the added order at the cost of slower running times.

The HashSet class in C# goes for the first approach, thus not preserving the order of elements. It is much faster than a regular List. Some basic benchmarks showed that HashSet is decently faster when dealing with primary types (int, double, bool, etc.). It is a lot faster when working with class objects. So that point is that HashSet is fast.

The only catch of HashSet is that there is no access by indices. To access elements you can either use an enumerator or use the built-in function to convert the HashSet into a List and iterate through that. Take a look here

like image 94
kamaci Avatar answered Oct 20 '22 13:10

kamaci