Skip to main content Link Search Menu Expand Document (external link)
Table of contents
  1. Associative Arrays (AA)
    1. AA Operations
      1. Removing keys:
      2. Testing membership:
      3. Practice

Associative Arrays (AA)

Associative arrays represent the D language hashtable implementation and have the following syntax:

void main()
{
    // Associative array of ints that are indexed by string keys.
    // The KeyType is string.
    int[string] aa;

    // set value associated with key "hello" to 3
    aa["hello"] = 3;
    int value = aa["hello"];
}

AA Operations

Removing keys:

void main()
{
    int[string] aa;
    aa["hello"] = 3;

    aa.remove("hello")     // removes the pair ("hello", 3)
}

remove(key) does nothing if the given key does not exist and returns false. If the given key does exist, it removes it from the AA and returns true. All keys can be removed by using the method clear.

Testing membership:

void main()
{
    int[string] aa;
    aa["hello"] = 3;

    int* p;
    p = "hello" in aa;

    if (p)
    {
        *p = 4;  // update value associated with key; aa["hello"] == 4
    }
}

The “in” expression yields a pointer to the value if the key is in the associative array, or null if not.

For more advanced operations on AAs check this link. For an exhaustive list of the AA properties check this link.

Practice

  1. Find the majority element in a string array using builtin associative arrays. You can start your implementation using the skeleton inside practice/majority-element directory.

  2. Go to this link. You will find a series of searching algorithms, each implemented in C in its own file.

    • Choose one algorithm and port it to D with minimum modifications.
    • Update the code using D specific features to improve the code (fewer lines of code, increase in expressiveness etc.).