Open In App

Differences between Array and Dictionary Data Structure

Arrays:

Array representation:




// C++ code for creating an array
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
   
    // Creating an array
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
   
    // Printing the array
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
  public static void main (String[] args) {
 
    // Creating an array
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
 
    // Printing the array
    for (int i = 0; i < 10; i++){
      System.out.print(arr[i]+" ");
    }
  }
}
 
// This code is contributed by aadityaburujwale.




# Python code for Creation of Array
 
# importing "array" for array creation
import array as arr
 
# creating an array with integer type
a = arr.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
 
# printing the array
for i in range(0, 10):
   
    # comma is used to generate spaces
    print(a[i]),




// C# code for creating an array
using System;
 
// Driver Code
public class GFG {
 
    static public void Main()
    {
       
        // Declaring an array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
        // Printing the array
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i] + " ");         
        }
    }
}




<script>
     
    // Creating an array
    let arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
     
    // Printing the array
    for(let i = 0; i < 10; i++){
        document.write(arr[i] + " ");
    }
     
    // This code is contributed by lokesh
     
</script>

Output
1 2 3 4 5 6 7 8 9 10 

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



Dictionary:

Dictionary representation:




// C++ program to demonstrate functionality of unordered_map
#include <iostream>
#include <unordered_map>
using namespace std;
 
int main()
{
     // Declare a Dictionary
    unordered_map<string, int> my_dict;
 
    // Adding Elements to the Dictionary
        my_dict["key1"] = 1;
        my_dict["key2"] = 2;
        my_dict["key3"] = 3;
 
    // Printing the Dictionary
    for (auto key : my_dict)
        cout << "Key: " << key.first << " Value: " << key.second << endl;
 
}
 
// This code is contributed by aadityaburujwale.




/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main (String[] args) {
        
          // Declare a Dictionary
        HashMap<String,Integer> my_dict = new HashMap<>();
       
         // Adding Elements to the Dictionary
        my_dict.put("key1", 1);
        my_dict.put("key2", 2);
        my_dict.put("key3", 3);
 
          // Printing the Dictionary
        for(String key:my_dict.keySet()) {
              System.out.println("Key: "+ key +", Value: " + my_dict.get(key));
            }
    }
}




# Declaring and initializing a dictionary
my_dict = {
  "key1": 1,
  "key2": 2,
  "key3": 3
}
# Printing the dictionary
 
print(my_dict)




using System;
using System.Collections.Generic;
public class GFG {
 
    static public void Main()
    {
     // Declare a Dictionary
        IDictionary<string, int> my_dict= new Dictionary<string, int>();
       
     // Adding Elements to the Dictionary
        my_dict.Add("key1", 1);
        my_dict.Add("key2", 2);
        my_dict.Add("key3", 3);
 
      // Printing the Dictionary
        foreach(var kvp in my_dict) {
          Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
    }
  }
}




<script>
// Javascript program to demonstrate functionality of map
 
// Declare a Dictionary
var my_dict = new Map();
 
// Adding Elements to the Dictionary
my_dict.set("key1", 1);
my_dict.set("key2", 2);
my_dict.set("key3", 3);
 
// Printing the Dictionary
console.log(my_dict);
 
// This  code is contributed by Shubham Singh
</script>

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

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



Comparison Between Array and Dictionary:

# Array Dictionary 
1 Stores just a set of objects Represents 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) 

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

Article Tags :