Open In App

Differences between Array and Dictionary Data Structure

Last Updated : 08 May, 2023
Comments
Improve
Suggest changes
3 Likes
Like
Report

Arrays:

  • The array is a collection of the same type of elements at contiguous memory locations under the same name. 
  • It is easier to access the element in the case of an array. 
  • The size is the key issue in the case of an array which must be known in advance so as to store the elements in it. 
  • Insertion and deletion operations are costly in the case of an array since the elements are stored at contiguous memory locations. 
  • No modification is possible at the runtime after the array is created and memory wastage can also occur if the size of the array is greater than the number of elements stored in the array.

Array representation:

C++
Java Python C# JavaScript

Output
1 2 3 4 5 6 7 8 9 10 

Time Complexity: O(1)
Auxiliary Space: O(1)

Dictionary:

  • A dictionary is a collection of data values. 
  • It holds a key: value pair in which we can easily access a value if the key is known. 
  • It improves the readability of your code and makes it easier to debug
  • It is fast as the access of a value through a key is a constant time operation

Dictionary representation:

C++
Java Python C# JavaScript

Output
{'key3': 3, 'key2': 2, 'key1': 1}

Time Complexity: O(1)
Auxiliary Space: O(1)

Comparison Between Array and Dictionary:

#ArrayDictionary 
1Stores just a set of objectsRepresents the relationship between pair of objects
2

Lookup time is more in the  case of array O(N) 

where N is the size of the array

Lookup time is less compared to an array.

Generally, it is O(1) 

3Elements are stored at contiguous memory locations.Elements may or may not be stored at a contiguous memory location.
4Items are unordered, changeable, and do allow duplicatesItems are ordered, changeable, and do not allow duplicates
5Items are not represented as key: value pairItems are represented as key: value pair
6The values in the array are of the same data typeThe values in dictionary items can be of any data type
7Values can be accessed randomly without the need for any keyTo access a value the key is required 

Next Article

Similar Reads