2273 Dictionaries

From Coder Merlin
Jump to navigation Jump to search
ComingSoonIcon.png
Coming Soon
Dictionaries

Dictionaries[edit]

Arrays are a collection type and enable us to quickly and easily access an element of the collection at a specific index, identified by an integer offset from the beginning of the collection. While arrays are very fast, they don’t provide us with the ability to quickly access an element given a key of arbitrary type.

Dictionaries are a type of hash table. A hash table is a data structure which implements the abstract data type associative array. The hash table uses a hash function to compute an index into an array of slots at which the element can be found. In a well-implemented algorithm, access to the element can occur in O(1).

n some cases, a hash function may not produce unique values. This is termed a collision. Collisions need to be appropriately resolved in order to access the proper element.

var d = Dictionary<String, Int>()
d[“Jack”] = 100
d[“Jill”] = 200

print(d[“Jill”]!)


Designed with pride in Silicon Valley, CA, USA