Search code examples
arraysdictionarylanguage-featuresdata-structures

Array/list vs Dictionary (why we have them at first place)


To me they are both same and that is why i am wondering why we have dictionary data structure when we can do everything with arrays/list? What is so fancy in dictionaries?


Solution

  • Arrays provide random access of a sequential set of data. Dictionaries (or associative arrays) provide a map from a set of keys to a set of values.

    I believe you are comparing apples and oranges - they serve two completely different purposes and are both useful data structures.

    Most of the time a dictionary-like type is built as a hash table - this type is very useful as it provides very fast lookups on average (depending on the quality of the hashing algorithm).