Differences between Array and Dictionary Data Structure Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article 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++ // 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; } Java /*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 # 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# // 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] + " "); } } } JavaScript <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> Output1 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 debugIt is fast as the access of a value through a key is a constant time operationDictionary representation: C++ // 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. Java /*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)); } } } Python # Declaring and initializing a dictionary my_dict = { "key1": 1, "key2": 2, "key3": 3 } # Printing the dictionary print(my_dict) C# 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); } } } JavaScript <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:#ArrayDictionary 1Stores just a set of objectsRepresents the relationship between pair of objects2 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 duplicates5Items are not represented as key: value pairItems are represented as key: value pair6The values in the array are of the same data typeThe values in dictionary items can be of any data type7Values can be accessed randomly without the need for any keyTo access a value the key is required Comment More infoAdvertise with us Next Article Differences between Array and Dictionary Data Structure K kishanpandeyrkt Follow Improve Article Tags : Data Structures Difference Between DSA Arrays Java-Dictionary +1 More Practice Tags : ArraysData Structures Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like