Dictionaries

From Coder Merlin
Revision as of 08:03, 25 October 2019 by Chukwuemeka-tinashe (talk | contribs) (Created page with "{{ComingSoon|Dictionaries}} == Dictionaries == Arrays are a collection type and enable us to quickly and easily access an element of the collection at a specific index, ident...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
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”]!)