Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worst case complexity of creating a HashSet<int> from a collection

I have a collection of int values with which I populate a HashSet<int> in the following manner -

var hashSet = new HashSet<int>(myIEnumerable);

Assuming that iterating the IEnumerable is O(n), what will be the worst case complexity of creating a HashSet<int> in such a way?

like image 295
UghSegment Avatar asked Dec 28 '12 15:12

UghSegment


People also ask

What is the time complexity of the HashSet?

For HashSet, LinkedHashSet, and EnumSet, the add(), remove() and contains() operations cost constant O(1) time thanks to the internal HashMap implementation. Likewise, the TreeSet has O(log(n)) time complexity for the operations listed in the previous group. This is because of the TreeMap implementation.

Why HashSet contains is o 1?

On average, the contains() of HashSet runs in O(1) time. Getting the object's bucket location is a constant time operation. Taking into account possible collisions, the lookup time may rise to log(n) because the internal bucket structure is a TreeMap.

What is HashSet in c#?

A HashSet is an optimized collection of unordered, unique elements that provides fast lookups and high-performance set operations. The HashSet class was first introduced in . NET 3.5 and is part of the System. Collection. Generic namespace.


2 Answers

You can bring the worst case to O(N^2) by supplying objects that all hash to the same bucket when the set reaches its maximum size. For example, if you pass a sequence of 17519 ints constructed as

x[i] = i * 17519

for i between 1 and 17519, inclusive, all numbers will hash to the initial bucket on Microsoft's implementation of HashSet<int>, taking O(N^2) to insert:

var h = new HashSet<int>(Enumerable.Range(1, 17519).Select(i => i*17519));

Set a brea kpoint, and examine h in the debugger. Look at Raw View / Non-public members / m_buckets. Observe that the initial bucket has 17519 elements, while the remaining 17518 all have zeros.

like image 20
Sergey Kalinichenko Avatar answered Sep 22 '22 04:09

Sergey Kalinichenko


The documentation actually states:

This constructor is an O(n) operation, where n is the number of elements in the collection parameter.

http://msdn.microsoft.com/en-us/library/bb301504.aspx

like image 144
Robert Levy Avatar answered Sep 23 '22 04:09

Robert Levy