ArticleslgStudy

science

Associative array

Associative array is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Associative array rather than just read about it. In short: In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of key/value pairs, such that each possible key appears at most once in the collection. In mathematical terms, an associative array is a function with finite domain.

Associative array — main illustration
Associative array — illustration

Key takeaways

  • Associative array belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Associative array to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Associative array from memory before moving on to harder problems.

Reference excerpt

In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of key/value pairs, such that each possible key appears at most once in the collection. In mathematical terms, an associative array is a function with finite domain. It supports 'lookup', 'remove', and 'insert' operations. The dictionary problem is the classic problem of designing efficient data structures that implement associative arrays. The two major solutions to the dictionary problem are hash tables and search trees. It is sometimes also possible to solve the problem using directly addressed arrays, binary search trees, or other more specialized structures. Many programming languages include associative arrays as primitive data types, while many other languages provide software libraries that support associative arrays. Content-addressable memory is a form of direct hardware-level support for associative arrays. Associative arrays have many applications including such fundamental programming patterns as memoization and the decorator pattern. The name does not come from the associative property known in mathematics. Rather, it arises from the association of values with keys. It should not be confused with associative processors.

Operations In an associative array, the association between a key and a value is often known as a "mapping"; the same word may also be used to refer to the process of creating a new association. The operations that are usually defined for an associative array are:

Insert or put add a new (key,value) pair to the collection, mapping the key to its new value. Any existing mapping is overwritten. The arguments to this operation are the key and the value. Remove or delete remove a (key,value) pair from the collection, unmapping a given key from its value. The argument to this operation is the key. Lookup, find, or get find the value (if any) that is bound to a given key. The argument to this operation is the key, and the value is returned from the operation. If no value is found, some lookup functions raise an exception, while others return a default value (such as zero, null, or a specific value passed to the constructor). Associative arrays may also include other operations such as determining the number of mappings or constructing an iterator to loop over all the mappings. For such operations, the order in which the mappings are returned is usually implementation-defined. A multimap generalizes an associative array by allowing multiple values to be associated with a single key. A bidirectional map is a related abstract data type in which the mappings operate in both directions: each value must be associated with a unique key, and a second lookup operation takes a value as an argument and looks up the key associated with that value.

Properties The operations of the associative array should satisfy various properties:

lookup(k, insert(j, v, D)) = if k == j then v else lookup(k, D) lookup(k, new()) = fail, where fail is an exception or default value remove(k, insert(j, v, D)) = if k == j then remove(k, D) else insert(j, v, remove(k, D)) remove(k, new()) = new() where k and j are keys, v is a value, D is an associative array, and new() creates a new, empty associative array.

Example Suppose that the set of loans made by a library is represented in a data structure. Each book in a library may be checked out by one patron at a time. However, a single patron may be able to check out multiple books. Therefore, the information about which books are checked out to which patrons may be represented by an associative array, in which the books are the keys and the patrons are the values. Using notation from Python or JSON, the data structure would be:

A lookup operation on the key "Great Expectations" would return "John". If John returns his book, that would cause a deletion operation, and if Pat checks out a book, that would cause an insertion operation, leading to a different state:

Implementation For dictionaries with very few mappings, it may make sense to implement the dictionary using an association list, which is a linked list of mappings. With this implementation, the time to perform the basic dictionary operations is linear in the total number of mappings. However, it is easy to implement and the constant factors in its running time are small. Another very simple implementation technique, usable when the keys are restricted to a narrow range, is direct addressing into an array: the value for a given key k is stored at the array cell A[k], or if there is no mapping for k then the cell stores a special sentinel value that indicates the lack of a mapping. This technique is simple and fast, with each dictionary operation taking constant time. However, the space requirement for this structure is the size of the entire keyspace, making it impractical unless the keyspace is small. The two major approaches for implementing dictionaries are a hash table or a search tree.

Hash table implementations

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Associative array

Start with the simplest possible case. Write down what Associative array claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Associative array before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Associative array ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Associative array

In research
Associative array appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Associative array in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Associative array is common in secondary-school and first-year university syllabi. It links to neighbouring topics Abstract data types, Associative arrays, Composite data types, so understanding it makes those chapters shorter.
In everyday life
Look for Associative array outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Associative array” →

How to study Associative array in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Associative array means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Associative array out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Associative array in simple terms?

In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of key/value pairs, such that each possible key appears at most once in the collection. In mathematical terms, an associative array is a function with finit…

Why does Associative array matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Associative array?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Associative array.

Tags

  • Abstract data types
  • Associative arrays
  • Composite data types
  • Data types

Keep exploring