What is an Associative Array?

πŸ’‘ Concept Name

Associative Array β€” A data structure that lets you store and retrieve values using custom keys instead of just numbers. Also known as a dictionary or map in most programming languages.

πŸ“˜ Quick Intro

An associative array lets you find information not by position, but by a special key you choose (like a name or code). In C# and .NET, this powerful idea shows up as Dictionary<TKey, TValue>.

🧠 Analogy / Short Story

Picture your phone’s contact list. You never scroll by numberβ€”you type in the name (the key) and instantly get their number (the value). Associative arrays bring this convenience to programming!

πŸ”§ Technical Explanation

  • πŸ”‘ Values are stored as key-value pairs, not just in order.
  • ⚑ Key lookups are super fastβ€”usually O(1) thanks to hashing.
  • πŸ›‘οΈ Keys must be unique (no duplicates allowed), but values can repeat.
  • 🌎 Keys can be any hashable typeβ€”string, int, even custom objects.
  • πŸ› οΈ Backed by a hash table (or sometimes a balanced tree for sorted maps).

🎯 Purpose & Use Case

  • βœ… Lookup tables (country codes, product IDs, settings).
  • βœ… Caching results for fast reuse.
  • βœ… Counting or tallying (like word frequencies).
  • βœ… Storing configurations, user preferences, or feature toggles.

πŸ’» Real Code Example

// Creating and using a dictionary
var capitals = new Dictionary<string, string> {
    { "USA", "Washington, D.C." },
    { "UK", "London" },
    { "India", "New Delhi" }
};

Console.WriteLine(capitals["India"]); // Output: New Delhi

❓ Interview Q&A

Q1: What is an associative array?
A: A structure where each value is linked to a unique key.

Q2: What is the .NET equivalent?
A: Dictionary<TKey, TValue> in C#.

Q3: Can keys repeat in a dictionary?
A: No, every key must be unique.

Q4: Can values repeat?
A: Yes, values can be anythingβ€”even duplicated.

Q5: What is the average time complexity for key lookup?
A: O(1), thanks to hash tables.

Q6: Is the order of keys preserved?
A: No, unless you use OrderedDictionary or SortedDictionary.

Q7: Can dictionaries grow or shrink as you add/remove entries?
A: Yes, they resize as needed.

Q8: What if you add a duplicate key?
A: It throws an exception at runtime.

Q9: How is a dictionary implemented in .NET?
A: With a hash table under the hood.

Q10: Are string keys case-sensitive in C#?
A: Yes, unless you use a custom comparer.

πŸ“ MCQs

Q1. What does an associative array use for indexing?

  • Numbers
  • Keys
  • Booleans
  • Pointers

Q2. Which C# type represents an associative array?

  • List
  • Array
  • Dictionary
  • Queue

Q3. What is true about keys in associative arrays?

  • They can be duplicated
  • They must be unique
  • They must be integers
  • They must be lowercase

Q4. What is the average time complexity for a key lookup in Dictionary?

  • O(1)
  • O(n)
  • O(log n)
  • O(n log n)

Q5. Can values be duplicated in a Dictionary?

  • Yes
  • No
  • Only integers
  • Only strings

Q6. Which of the following is NOT an associative array?

  • Dictionary
  • Map
  • Hashtable
  • Stack

Q7. Which data structure allows fast access using a key?

  • Array
  • Stack
  • Associative Array
  • List

Q8. Which statement is correct about .NET Dictionary?

  • It is implemented using arrays
  • It uses trees
  • It is implemented using hash tables
  • It is unordered list

Q9. Can associative arrays grow dynamically?

  • No
  • Yes
  • Only up to 1000 items
  • Only during compile time

Q10. What happens if you insert duplicate keys?

  • Values are merged
  • Both keys are stored
  • Exception is thrown
  • Older value is kept

πŸ’‘ Bonus Insight

Try TryGetValue() to safely fetch a value without risking an exception if the key isn’t found. For multi-threaded code, use ConcurrentDictionary for built-in thread safety.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Arrays πŸ“š

What is an Array? πŸ‘‰ Learn More
Characteristics of Array in Data Structure πŸ‘‰ Learn More
Advantages & Disadvantages of Array πŸ‘‰ Learn More
Dynamic Arrays: How They Work πŸ‘‰ Learn More
Time Complexity of Array Operations πŸ‘‰ Learn More
Array vs Dictionary πŸ‘‰ Learn More
Associative Array πŸ‘‰ Learn More
Sorted Arrays & Advantages πŸ‘‰ Learn More
How Indexing Works in Arrays πŸ‘‰ Learn More
Array vs Linked List Stack Implementation πŸ‘‰ Learn More
Share:

Tags:


Feedback Modal Popup