What is an Array in Data Structures? Key Concepts, Types, and Operations
By Rohan Vats
Updated on Feb 03, 2025 | 21 min read | 8.9k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Feb 03, 2025 | 21 min read | 8.9k views
Share:
Table of Contents
Arrays are a fundamental data structure, storing a collection of elements in contiguous memory, which allows for fast random access to elements. However, their fixed size can limit flexibility, requiring careful memory allocation during design.
Arrays form the foundation for many data structures (e.g., stacks, queues) and algorithms, serving as a baseline for tasks like sorting and searching.
In this blog, we’ll dive into the key concepts of array in data structure, their types, and the common operations performed on them.
An array is a data structure that stores a collection of data items (called elements) of the same type, such as contact numbers of customers visiting a store. This structured layout ensures that each element can be stored, accessed, and updated using a unique index starting from 0.
Accessing elements by their index is particularly efficient, with a time complexity of O(1), making arrays ideal for scenarios requiring quick lookups or updates. The number of elements in an array is denoted by its array length.
For example, if VMart decides to offer a 10% discount to customers who visited the store last month, they can access relevant data. Similarly, in weather monitoring systems, arrays are used to store hourly temperature readings for quick analysis.
Basic Concept of Arrays:
In contiguous memory allocation, when a process requests memory, the operating system assigns it a single block of memory that is adjacent in physical memory. This approach can be implemented using either fixed-size or variable-size partitions:
Arrays are widely used in various domains because of their versatility and efficiency. Here are some common use cases of array in data structure:
For example, a weekly planner might use an array to store tasks for each day: weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]. This makes it easy to iterate through the days and assign tasks accordingly.
For example, a simple image might be represented as image[][] = [[255, 0, 0], [0, 255, 0], [0, 0, 255]], where each sub-array corresponds to a row of pixels.
Arrays have a fixed size, meaning the number of elements is defined at creation and cannot be changed during runtime. They store homogeneous elements in contiguous memory locations, allowing random access through indexing.
Arrays and linked lists are both used to store collections of data but how they differ to each other are as follows:
Identifier | Arrays | Linked Lists |
Structure | Static in size (fixed at creation), and all elements are in a contiguous block of memory. | Dynamic; they are made up of nodes, where each node contains data and a pointer to the next node. The nodes can be scattered in memory, so they don't need to be contiguous. |
Access Time | Accessing an element is fast (O(1)) | Accessing an element requires traversing the list from the head node, so it takes O(n) time in the worst case. |
Insertion/Deletion | An array requires shifting elements around, which can take O(n) time. | In a linked list, insertion or deletion only requires adjusting the pointers, which takes O(1) time, |
Memory Efficiency | Arrays use memory efficiently (no overhead like pointers) | Linked lists require extra memory for the pointers, making them more memory-intensive. |
Cache Locality | Better in terms of cache locality | Less locality, |
Also Read: Array in Data Structure - Explanation, Function & Examples
Arrays enable efficient random access to elements via their index, making them ideal for tasks requiring quick lookups or updates.
A static array is an array where the size is defined at the time of creation and cannot be changed during runtime. The memory for static arrays is allocated once, and its size is fixed for the entire duration of the program.
Potential Limitations:
Also Read: Creating a Dynamic Array in Java
Dynamic arrays are arrays that allow for resizing during runtime. Unlike static arrays, dynamic arrays can grow or shrink based on the needs of the program, which makes them more flexible and useful in scenarios where the number of elements is not known in advance.
Dynamic Array Libraries or Structures:
Dynamic arrays are widely used in modern programming languages due to their flexibility and ease of use, particularly when the exact size of data is unknown at the time of writing the program. Despite the slight memory overhead and occasional resizing cost, they provide the best of both worlds: the speed of array indexing and the flexibility of resizing.
Also Read: Dynamic Array Creation in C Language
Now that we’ve grasped the fundamentals, let’s dive deeper into the different types of arrays and how they vary in functionality.
Arrays efficiently store data in memory. One-dimensional arrays manage simple lists, multi-dimensional arrays handle grids (e.g., matrices, images), and jagged arrays excel in managing irregular datasets or hierarchical structures.
Let’s start by exploring one of the simplest and most commonly used types of array in data structures, One-Dimensional Arrays.
One-dimensional arrays are the simplest type of array where elements are arranged in a single line. This means that all the elements are indexed sequentially, starting from index 0.
Example:
Array of student marks.
int marks []= {85, 90, 76, 88, 92}
List of numbers.
int num [] ={5, 12, 7, 20}
Here, each element in the array is accessed using a single index (e.g., array[0] for the first element).
A two-dimensional array is an array of arrays, where each element is itself an array. It can be thought of as a grid or table with rows and columns.
Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
A multi-dimensional array is an extension of two-dimensional arrays, where you can have three or more dimensions. These arrays are useful when working with complex data that has multiple layers or axes.
Example: A 3D array could be a cube of values:
int arr[2][2][2] = { { { 5, 2 }, { 7, 1 } },
{ { 3, 6 }, { 11, 21 } } };
Explanation:
Use Cases:
Multi-dimensional arrays allow you to organize and access large, complex data in a structured manner.
Also Read: Multidimensional Array in PHP [With Examples]
A jagged array is an array of arrays where each "inner array" can have a different length. Unlike a two-dimensional array, which has a consistent number of columns in every row, jagged arrays can have rows with varying numbers of elements.
[
[1, 2, 3],
[4, 5],
[6, 7, 8, 9]
]
If you are interested in learning more about arrays and data structures, then check out this free upGrad course on Data Structures and Algorithms!
Having covered the various array types, it’s crucial to understand how memory is allocated for arrays to optimize their performance.
Arrays are a fundamental data structure in computer science, and their memory allocation plays a crucial role in their efficiency and performance. The memory allocation of arrays can differ significantly between static arrays and dynamic arrays.
Arrays in different programming languages have specific syntaxes for declaration. Here's how you can declare arrays in various languages:
Arrays in C or C++, are declared by specifying the type, followed by the array name and the number of elements.
int array[5]; // Declares an array of 5 integers
Also Read: Top 7 Most Powerful Features of C++ You Should Know About
Arrays in Java are declared using the type, followed by square brackets.
int[] array = new int[5]; // Declares an array of 5 integers
In Python, arrays are usually represented by lists. Python doesn't require you to specify the number of elements at the time of declaration.
array = [1, 2, 3, 4, 5] # A list with 5 elements
JavaScript arrays are dynamic and can hold elements of any type. You declare them using square brackets.
let array = [1, 2, 3, 4, 5]; // Declares an array of 5 elements
Array elements are accessed by their index, which starts from 0 for the first element. The formula to calculate the address of an array element is:
address_of_element = base_address + (index * size_of_element)
Where:
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 3, 4, 5};
cout << "Element at index 2: " << array[2] << endl; // Accessing the 3rd element (index 2)
return 0;
}
Output:
Element at index 2: 3
In this example, array[2] accesses the third element of the array (value 3), as array indexing starts from 0.
Arrays can be initialized either at the time of declaration or later during program execution. In most cases, values are assigned to an array using index-based access.
int array[5] = {1, 2, 3, 4, 5}; // Initialize array with values at declaration
let array = [];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5; // Initialize array with values using index-based access
array = [1, 2, 3, 4, 5] # Initialize list with values at the time of declaration
Also Read: The Ultimate C++ Guide: C++ Tutorials for Beginners
With memory allocation clarified, let's move on to understanding the essential operations performed on arrays and their time complexities.
upGrad’s Exclusive Data Science Webinar for you –
Watch our Webinar on How to Build Digital & Data Mindset?
Arrays are fundamental data structures that provide efficient ways to store and manipulate collections of data. There are several key operations performed on arrays to manage and access the data. These operations are essential for tasks like adding new elements, deleting existing ones, searching for specific values, and updating data.
The common operations on arrays include:
Traversal refers to visiting every element in the array. It’s often the first step when performing operations like searching or updating an array, as it allows us to access each element in sequence.
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 3, 4, 5};
// Traversing the array
for(int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << array[i] << endl;
}
return 0;
}
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
In the above code, we use a loop to visit and print each element in the array, starting from index 0 to 4.
Insertion involves adding a new element to the array. It can be done at:
#include <iostream>
using namespace std;
int main() {
int array[6] = {1, 2, 3, 4, 5};
int size = 5;
int new_element = 6;
// Insertion at the end
array[size] = new_element;
size++;
// Traversing after insertion
for(int i = 0; i < size; i++) {
cout << array[i] << " ";
}
return 0;
}
Output:
1 2 3 4 5 6
Here, we insert the new element 6 at the end of the array and then print the array.
Deletion removes an element from the array. In dynamic arrays or static arrays, deletion requires shifting the subsequent elements to fill the gap left by the deleted element.
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 3, 4, 5};
int size = 5;
int index_to_delete = 2; // Deleting element at index 2 (value 3)
// Deletion: Shift elements left
for(int i = index_to_delete; i < size - 1; i++) {
array[i] = array[i + 1];
}
size--; // Decrease the size of the array
// Traversing after deletion
for(int i = 0; i < size; i++) {
cout << array[i] << " ";
}
return 0;
}
Output:
1 2 4 5
In this example, after deleting the element at index 2, the elements to the right of it are shifted left.
Search is used to find the index of an element in the array. If the element is found, the index is returned; otherwise, a signal (like -1 or null) is returned to indicate the element is not present.
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 3, 4, 5};
int size = 5;
int target = 3;
int index = -1; // Initializing index to -1, assuming element not found
// Linear search
for(int i = 0; i < size; i++) {
if(array[i] == target) {
index = i;
break;
}
}
cout << "Element " << target << " found at index: " << index << endl;
return 0;
}
Output:
Element 3 found at index: 2
Here, we perform a linear search to find the element 3 and return its index (2).
Explanation: Update modifies the value of an existing element in the array. You access the element using its index and assign a new value to it.
#include <iostream>
using namespace std;
int main() {
int array[5] = {1, 2, 3, 4, 5};
int index_to_update = 2;
int new_value = 10;
// Update the element at index 2
array[index_to_update] = new_value;
// Traversing after update
for(int i = 0; i < 5; i++) {
cout << array[i] << " ";
}
return 0;
}
Output:
1 2 10 4 5
In this example, the element at index 2 is updated to 10, and the updated array is printed.To casual please frame a better CTA
Also Read: Array in Java: What You Need To Know?
Each operation on an array comes with its own time and space complexity. Below is an analysis of the time complexities for common array operations:
Operation | Time Complexity |
Space Complexity | |
1-D Array | 2-D Array | ||
Traversal | O(n) | O(m * n) | O(1) |
Accessing an Element | O(1) | O(1) | O(1) |
Insertion (end) | O(1) (amortized) | O(1) (amortized) | O(1) |
Insertion (beginning) | O(n) | O(m * n) | O(n) |
Deletion | O(n) | O(m * n) | O(1) |
Searching | O(n) | O(m * n) | O(1) |
Also Read: How to Convert Object to Array in PHP
Now that we’ve analyzed the operations, it’s important to evaluate the advantages and disadvantages of using arrays in data structures.
Array in data structure that offers efficient storage and quick access to elements through indexing. However, their fixed size and the need for shifting elements during insertions or deletions can limit flexibility and performance.
Here are some of the advantages:
Advantages | Description |
Fast Access to Elements | Time Complexity: O(1) Arrays allow quick and direct access to elements using their index, which makes accessing individual elements very efficient. |
Simple and Easy to Implement | Arrays are one of the simplest data structures, and their implementation does not require complex memory management or pointers (in languages like Python or Java). |
Efficient Memory Usage (for Fixed Data) | In static arrays, memory is allocated at the time of declaration, making it efficient for storing fixed amounts of data. |
Contiguous Memory Allocation | Elements in arrays are stored in consecutive memory locations, which improves performance in terms of cache utilization. |
Efficient for Storing Homogeneous Data | Arrays are ideal for storing collections of the same type of data, making them a natural fit for use cases like numerical computations, matrices, and other scenarios where elements are similar. |
Random Access | Arrays support random access to elements, which means that any element can be accessed directly in constant time using its index. |
Here are some of the disadvantages of arrays:
Disadvantages | Description |
Fixed Size (in Static Arrays) | Once the size of a static array is declared, it cannot be changed during runtime. This can lead to wasted memory if the array is underutilized, or insufficient space if more elements are needed.
|
Inefficient for Dynamic Data (in Static Arrays) | If the size of the data set is not known in advance, a static array may require resizing or restructuring, which can be inefficient. |
Memory Overhead (in Dynamic Arrays) | Dynamic arrays, while resizable, may incur overhead when they resize. To accommodate new elements, a new memory block may be allocated, and old elements need to be copied, leading to performance penalties. |
Insertion and Deletion Complexity | Time Complexity: O(n) Inserting or deleting elements in the middle of an array requires shifting elements to maintain contiguous memory allocation. This can be inefficient for large arrays. |
Limited Flexibility with Non-Homogeneous Data | Arrays require all elements to be of the same type, which can limit flexibility when storing heterogeneous data or more complex objects. |
Memory Wastage (for Arrays of Unknown Size) | In scenarios where the size of the array is unknown or highly variable, either too much memory is allocated (if the size is overestimated), or resizing becomes a costly operation (if the size is underestimated). |
Also Read: Python Array vs. List: Difference Between Array and List in Python: Key Insights
After understanding the advantages and disadvantages of arrays, let’s see how platforms like upGrad can help you leverage these concepts to advance your career.
Understanding array in data structures and learning programming language is a crucial step in becoming a proficient full stack developer, and upGrad offers a range of programs to help you achieve this.
The courses combine hands-on training, real-world projects, and personalized mentorship to help you advance your career and gain in-demand skills.
Here are some relevant courses you can check out:
Enroll now and start mastering array in data structure and other web development skills with upGrad. Get personalized counseling from upGrad’s experts or visit the nearest upGrad Career Centre to chart your path to success!
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources