Nepedia
Back to Discover
Advanced Reader
💻

Associative array

technology Maturity 11-13 Next article →

An associative array is an abstract data type used to store collections of key-value pairs. In computer science, these structures are also known as maps, dictionaries, symbol tables, or key-value stores. The fundamental rule is that each possible key can appear at most once in the collection. Mathematically, an associative array can be viewed as a function with a finite domain. These structures are vital because they allow computers to organize and retrieve specific pieces of information very quickly.

Hash table average insertion time
Hash table average insertion time

To manage these collections, programmers use three primary operations: insert, remove, and lookup. An insert or "put" operation adds a new pair to the collection by mapping a key to a specific value. If the key already exists, the new value overwrites the old one. A remove or "delete" operation unmaps a given key from its value, removing the pair entirely. Finally, a lookup, often called "find" or "get," uses a key to retrieve its bound value. If the key is not found, the system might return a default value like zero or null, or it might raise an exception.

There are several ways to implement these arrays, ranging from simple to highly complex. For very small collections, an association list—which is a linked list of mappings—is easy to build. However, finding an item in a list takes linear time, meaning it gets slower as the list grows. Another method is direct addressing, where a value is stored at a specific index in an array. This is extremely fast, providing constant time for operations, but it is impractical if the range of possible keys is very large. This is because the system must set aside space for every possible key, even if they are not used.

Most general-purpose associative arrays use a hash table. A hash table combines an array with a mathematical process called a hash function. This function takes a key and assigns it to a specific "bucket" within the array. Because accessing an array index is a constant-time operation, hash tables usually perform with an average time complexity of O(1). However, hash tables must handle collisions, which happen when two different keys map to the same bucket. To solve this, developers use separate chaining, where each bucket holds a list of all matching values, or open addressing, where the system searches for the next empty spot in the array.

Another major implementation method uses self-balancing binary search trees. Common examples include AVL trees and red-black trees. Unlike hash tables, these trees keep all elements in a sorted order. This allows for range queries, which are searches for all values between two specific bounds. While hash tables are faster on average, their worst-case performance can drop to O(n) if many keys collide in one bucket. In contrast, self-balancing trees guarantee a much better worst-case performance of O(log n). This makes them more predictable for certain types of complex data management.

History shows that the need for these structures has been present since the early days of computing. Built-in syntactic support for associative arrays was first introduced in 1969 by the language SNOBOL4, which called them "tables." Later, the language TMG provided tables using string keys, and MUMPS introduced multi-dimensional associative arrays. Today, most programming languages include these as primitive data types or provide them through extensive software libraries. They are essential for advanced programming patterns like memoization, which helps speed up calculations by storing previous results.

Associative arrays also have specialized variations to meet specific needs. A multimap is a generalization that allows a single key to be associated with multiple different values. A bidirectional map is a related structure that allows lookups to work in both directions. In a bidirectional map, every value is linked to a unique key, so you can use a value to find its corresponding key just as easily as using a key to find a value. These variations allow developers to choose the exact tool needed for the specific way their data must flow.

677 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (1)
File:Hash table average insertion time.png
Hash table average insertion time.png
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.