The Wayback Machine - https://web.archive.org/web/20241001072429/https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/
Open In App

Getting Started with Array Data Structure

Last Updated : 12 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Array is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. In this article, we have decided to provide a complete guide for Arrays, which will help you to tackle any problem based on Arrays.

Array-data-structure-2

What is an Array?

Array is a linear data structure that stores a collection of items of same data type in contiguous memory locations. Each item in an array is indexed starting with 0. We can directly access an array element by using its index value.

Basic terminologies of Array

  • Array Index: In an array, elements are identified by their indexes. Array index starts from 0.
  • Array element: Elements are items stored in an array and can be accessed by their index.
  • Array Length: The length of an array is determined by the number of elements it can contain. 

Memory representation of Array

In an array, all the elements are stored in contiguous memory locations. So, if we initialize an array, the elements will be allocated sequentially in memory. This allows for efficient access and manipulation of elements.

Memory-Representation-of-Array-(1)

Declaration of Array

Arrays can be declared in various ways in different languages. For better illustration, below are some language-specific array declarations:

C++
// This array will store integer type element
int arr[5];      
// This array will store char type element
char arr[10];   
// This array will store float type element
float arr[20];  
C
// This array will store integer type element
int arr[5];      
// This array will store char type element
char arr[10];   
// This array will store float type element
float arr[20];  
Java
// This array will store integer type element
int arr[];      
// This array will store char type element
char arr[];   
// This array will store float type element
float arr[];  
Python
import array

# This array will store integer type elements
arr_int = array.array('i', [1, 2, 3, 4, 5])
print("Integer Array:", arr_int.tolist())

# This array will store Unicode characters (Python 3.x)
# In Python 3, 'u' for Unicode is not supported in array module, so we use a list of characters.
# If you need to work with characters, you might use a list instead.
# For example:
arr_char = list('abc')  # List of characters
print("Character Array:", arr_char)

# This array will store float type elements
arr_float = array.array('f', [1.0, 2.0, 3.0])
print("Float Array:", arr_float.tolist())
C#
// This array will store integer type element
int[] arr;
// This array will store char type element
char[] arr2;
// This array will store float type element
float[] arr3;
Javascript
// JS code
let arr=[]

Initialization of Array

Arrays can be initialized in different ways in different languages. Below are some language-specific array initializations:

C++
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
C
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
Java
int arr[] = { 1, 2, 3, 4, 5 };
char arr[] = { 'a', 'b', 'c', 'd', 'e' };
float arr[] = { 1.4f, 2.0f, 24f, 5.0f, 0.0f };
Python
import array
# This array will store integer type element
arr = array.array('i', [1, 2, 3, 4, 5])
# This array will store char type element
arr = array.array('b', [])
# This array will store float type element
arr = array.array('f')
C#
int[] arr = { 1, 2, 3, 4, 5 };
char[] arr = { 'a', 'b', 'c', 'd', 'e' };
float[] arr = { 1.4f, 2.0f, 24f, 5.0f, 0.0f };
JavaScript
let arr = [ 1, 2, 3, 4, 5 ];
let arr = [ 'a', 'b', 'c', 'd', 'e' ];
let arr = [ 1.4, 2.0, 24, 5.0, 0.0 ];

Importance of Array

Assume there is a class of five students and if we have to keep records of their marks in examination then, we can do this by declaring five variables individual and keeping track of records but what if the number of students becomes very large, it would be challenging to manipulate and maintain the data.

What it means is that, we can use normal variables (v1, v2, v3, ..) when we have a small number of objects. But if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.

Importance-of-Array

Types of Arrays

Arrays can be classified in two ways:

  • On the basis of Size
  • On the basis of Dimensions
Types-of-Arrays


Types of Arrays on the basis of Size:

1. Fixed Sized Arrays:

We cannot alter or update the size of this array. Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage. In case, we don’t know the size of the array then if we declare a larger size and store a lesser number of elements will result in a wastage of memory or we declare a lesser size than the number of elements then we won’t get enough memory to store all the elements. In such cases, static memory allocation is not preferred.

C++
// Method 1 to create a fixed sized array. 
// Here the memory is allocated at compile time.
int arr[5]; 
// Another way (creation and initialization both)
int arr2[5] = {1, 2, 3, 4, 5}; 

// Method 2 to create a fixed sized array
// Here memory is allocated at run time (Also
// known as dynamically allocated arrays)
int *arr = new int[5]; 
C
// Method 1 to create a fixed sized array. 
// Here the memory is allocated at compile time.
int arr1[5];
// Another way (creation and initialization both)
int arr2[5] = {1, 2, 3, 4, 5}; 


// Method 2 to create a fixed sized array
// Here memory is allocated at run time (Also
// known as dynamically allocated arrays)
int *arr = (int*)malloc(n * sizeof(int));
Java
// Fixed sized array examples
int[] arr1 = new int [5];

// Another way (Array creation and 
// initialization both)
int[] arr2 = {1, 2, 3, 4, 5};
Python
import array

# Static Array
arr = array.array('i', [1, 2, 3, 4, 5])
C#
// Fixed sized array examples
int[] arr1 = new int [5];

// Another way (Array creation and 
// initialization both)
int[] arr2 = {1, 2, 3, 4, 5};


2. Dynamic Sized Arrays:

The size of the array changes as per user requirements during execution of code so the coders do not have to worry about sizes. They can add and removed the elements as per the need. The memory is mostly dynamically allocated and de-allocated in these arrays.

C++
#include<vector>

// Dynamic Integer Array
vector<int> v;
C
// C does not seem to support
// dynamic sized arrays as of now
Java
// Dynamic Integer Array
ArrayList<Integer> arr = new ArrayList<>();
Python
# Dynamic Array
arr = []
C#
// Similar to Java
ArrayList myList = new ArrayList(); 
JavaScript
// Dynamic Sized Array
let arr = new Array();  

Types of Arrays on the basis of Dimensions:

1. One-dimensional Array(1-D Array): You can imagine a 1d array as a row, where elements are stored one after another.

One-Dimensional-Array(1-D-Array)

2. Multi-dimensional Array: A multi-dimensional array is an array with more than one dimension. We can use multidimensional array to store complex data in the form of tables, etc. We can have 2-D arrays, 3-D arrays, 4-D arrays and so on.

Two-Dimensional-Array(2-D-Array-or-Matrix)

  • Three-Dimensional Array(3-D Array): A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.

Three-Dimensional-Array(3-D-Array)

Operations on Array

1. Array Traversal:

Array traversal involves visiting all the elements of the array once. Below is the implementation of Array traversal in different Languages:

C++14
int arr[] = { 1, 2, 3, 4, 5 };
int len = sizeof(arr) / sizeof(arr[0]);
// Traversing over arr[]
for (int i = 0; i < len; i++) {
    cout << arr[i] << " ";
C
int arr[] = { 1, 2, 3, 4, 5 };
int len = sizeof(arr) / sizeof(arr[0]);
// Traversing over arr[]
for (int i = 0; i < len; i++) {
    printf("%d ", arr[i]);
}
Java
int arr[] = { 1, 2, 3, 4, 5 };
// Traversing over arr[]
for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i] + " ");
Python
import array
arr = array.array('i', [1, 2, 3, 4, 5])
# Traversing over arr[]
for x in arr:
    print(x, end=" ")
C#
int[] arr = { 1, 2, 3, 4, 5 };
// Traversing over arr[]
for (int i = 0; i < arr.Length; i++)
    Console.Write(" " + arr[i]);
JavaScript
let arr = [1, 2, 3, 4, 5]
// Traversing over arr[]
for (let x of arr)
    console.log(x)

2. Insertion in Array:

We can insert one or multiple elements at any position in the array. Below is the implementation of Insertion in Array in different languages:

C++
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];
 
    arr[pos] = x;
}
C
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];

    arr[pos] = x;
}
Java
static void insertElement(int arr[], int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];
    arr[pos] = x;
}
Python
import array

def insertElement(arr, x, pos):
    n = len(arr)  # Current length of the array
    if pos < 0 or pos > n:  # Validate the position
        raise IndexError("Position out of range")
    
    # Create a new array with one more element
    new_arr = array.array(arr.typecode, [0] * (n + 1))
    
    # Copy elements to the new array
    for i in range(pos):
        new_arr[i] = arr[i]
    new_arr[pos] = x
    for i in range(pos, n):
        new_arr[i + 1] = arr[i]
    
    return new_arr

# Example usage
arr = array.array('i', [1, 2, 3, 4, 5])
x = 10  # Element to insert
pos = 2  # Position to insert the element

# Insert element
arr = insertElement(arr, x, pos)
print(arr.tolist())  # Convert to list for easy viewing
C#
static void insertElement(int[] arr, int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];
    arr[pos] = x;
}
JavaScript
// javascript Program to Insert an element
// at a specific position in an Array
function insertElement(arr, n, x, pos)
{
    // shift elements to the right
    // which are on the right side of pos
    var i = n - 1;
    for (i; i >= pos; i--)
    {
        arr[i + 1] = arr[i];
    }
    arr[pos] = x;
}

3. Deletion in Array:

We can delete an element at any index in an array. Below is the implementation of Deletion of element in an array:

C++
// To search a key to be deleted
int findElement(int arr[], int n, int key);

// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
    // Find position of element to be deleted
    int pos = findElement(arr, n, key);

    if (pos == -1) {
        cout << "Element not found";
        return n;
    }

    // Deleting element
    int i;
    for (i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];

    return n - 1;
}

// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
    // Return -1 if key is not found
    return -1;
}
C
// C program to implement delete operation in a
// unsorted array
#include <stdio.h>
 
// To search a key to be deleted
int findElement(int arr[], int n, int key);
 
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
    // Find position of element to be deleted
    int pos = findElement(arr, n, key);
 
    if (pos == -1) {
        printf("Element not found");
        return n;
    }
 
    // Deleting element
    int i;
    for (i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];
 
    return n - 1;
}
 
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
     // Return -1 if key is not found
    return -1;
}
Java
// function to search a key to
    // be deleted
    static int findElement(int arr[], int n, int key)
    {
        int i;
        for (i = 0; i < n; i++)
            if (arr[i] == key)
                return i;
         // Return -1 if key is not found
        return -1;
    }
 
    // Function to delete an element
    static int deleteElement(int arr[], int n, int key)
    {
        // Find position of element to be
        // deleted
        int pos = findElement(arr, n, key);
 
        if (pos == -1) {
            System.out.println("Element not found");
            return n;
        }
 
        // Deleting element
        int i;
        for (i = pos; i < n - 1; i++)
            arr[i] = arr[i + 1];
 
        return n - 1;
    }
Python
from array import array

# Function to search for a key in the array
def findElement(arr, key):
    for i in range(len(arr)):
        # Return the index if key is found
        if arr[i] == key:
            return i
    # Return -1 if key is not found
    return -1  

# Function to delete an element from the array
def deleteElement(arr, key):
    # Find position of element to be deleted
    pos = findElement(arr, key)

    if pos == -1:
        print("Element not found")
        return arr

    # Creating a new array with one less element
    new_arr = array(arr.typecode, [0] * (len(arr) - 1))
    
    # Copy elements except the one to be deleted
    for i in range(pos):
        new_arr[i] = arr[i]
    for i in range(pos + 1, len(arr)):
        new_arr[i - 1] = arr[i]

    return new_arr

# Example usage
arr = array('i', [1, 2, 3, 4, 5])
key_to_delete = 3

# Find the element
print(f"Index of {key_to_delete}: {findElement(arr, key_to_delete)}")

# Delete the element
arr = deleteElement(arr, key_to_delete)
print("Array after deletion:", arr.tolist())
C#
int findElement(int[] arr, int n, int key)
{

    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;

    return -1;
}

// Function to delete an element
int deleteElement(int[] arr, int n, int key)
{
    // Find position of element
    // to be deleted
    int pos = findElement(arr, n, key);

    if (pos == -1) {
        Console.WriteLine("Element not found");
        return n;
    }

    // Deleting element
    int i;
    for (i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];

    return n - 1;
}
JavaScript
// function to search a key to  be deleted
function findElement(arr,n,key)
{
    let i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
    return -1;
} 
     
// Function to delete an element
function deleteElement(arr,n,key)
{
    // Find position of element to be deleted
    let pos = findElement(arr, n, key);
      
    if (pos == -1)
    {
        document.write("Element not found");
        return n;
    }
    // Deleting element
    let i;
    for (i = pos; i< n - 1; i++)
        arr[i] = arr[i + 1];
    return n - 1;
}

4. Searching in Array:

We can traverse over an array and search for an element. Below is the implementation of Deletion of element in an array:

C++
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
 
    // If the key is not found
    return -1;
}
C
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
 
    // If the key is not found
    return -1;
}
Java
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
    for (int i = 0; i < n; i++)
        if (arr[i] == key)
            return i;

    // If the key is not found
    return -1;
}
Python
# Python program for searching in unsorted array

def findElement(arr, n, key):
    for i in range(n):
        if arr[i] == key:
            return i
    # If the key is not found
    return -1

# Example usage
if __name__ == "__main__":
    # Define an unsorted array
    arr = [4, 2, 7, 1, 9, 3]
    
    # Key to search for
    key = 7
    
    # Get the length of the array
    n = len(arr)
    
    # Call the function
    index = findElement(arr, n, key)
    
    # Print the result
    if index != -1:
        print(f"Element {key} found at index {index}.")
    else:
        print(f"Element {key} not found in the array.")
C#
// Function to implement
// search operation
int findElement(int[] arr, int n, int key)
{
    for (int i = 0; i < n; i++)
        if (arr[i] == key)
            return i;

    // If the key is not found
    return -1;
}
JavaScript
// Function to implement search operation 
function findElement( arr, n, key)
{
    let i;
    for (i = 0; i < n; i++)
        if (arr[i] == key)
            return i;
 
    return -1;
}

Complexity Analysis of Operations on Array

Time Complexity:

Operation

Best Case

Average Case

Worst Case

Traversal

ÎĐ(N)

Îļ(N)

O(N)

Insertion

ÎĐ(1)

Îļ(N)

O(N)

Deletion

ÎĐ(1)

Îļ(N)

O(N)

Searching

ÎĐ(1)

Îļ(N)

O(N)

Space Complexity:

Operation

Best Case

Average Case

Worst Case

Traversal

ÎĐ(1)

Îļ(1)

O(1)

Insertion

ÎĐ(1)

Îļ(N)

O(N)

Deletion

ÎĐ(1)

Îļ(N)

O(N)

Searching

ÎĐ(1)

Îļ(1)

O(1)

Advantages of Array

  • Arrays allow random access to elements. This makes accessing elements by position faster.
  • Arrays have better cache locality which makes a pretty big difference in performance.
  • Arrays represent multiple data items of the same type using a single name.
  • Arrays are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc.

Disadvantages of Array

  • As arrays have a fixed size, once the memory is allocated to them, it cannot be increased or decreased, making it impossible to store extra data if required. An array of fixed size is referred to as a static array. 
  • Allocating less memory than required to an array leads to loss of data.
  • An array is homogeneous in nature so, a single array cannot store values of different data types. 
  • Arrays store data in contiguous memory locations, which makes deletion and insertion very difficult to implement. This problem is overcome by implementing linked lists, which allow elements to be accessed sequentially.  

Applications of Array

  • They are used in the implementation of other data structures such as array lists, heaps, hash tables, vectors, and matrices.
  • Database records are usually implemented as arrays.
  • It is used in lookup tables by computer.

Conclusion

After the discussion, we concluded that arrays are a simple method of accessing elements of the same type by grouping them and we can find the elements efficiently by their indexes and can perform different operations using them. Thus, they are more efficient when it comes to memory allocation and should be used in all modern programming languages. So, this becomes a favorite topic for the perspective of the interview and most of the companies generally asked about the problems on the array. For all these reasons, we must have a good knowledge of it.

Related articles:

Frequently Asked Questions (FAQs) on Arrays

1. What is an array in data structure with example?

An array is a collection of items of the same data type stored at contiguous memory locations. Ex. int arr[5] = {1,2,3,4,5};

2. Why array is a data structure?

Arrays store elements of the same type, they are classified as homogeneous data structures. They can store numbers, strings, characters, boolean values (true and false), objects, and so on.

3. What data structure is an array?

 An array is a linear data structure that stores similar elements in contiguous memory locations.

4. What are the types of arrays?

There are majorly two types of arrays:

  • One dimensional array
  • Multidimensional array

5. How is data stored in an array?

An array is a collection of items of the same data type stored at contiguous memory locations or says the elements are stored one after another in memory. An array uses an index system starting at 0 and going to (n-1), where n is its size.

6. Difference between array and structure?

The structure can contain variables of different types but an array only contains variables of the same type. 

7. What are the limitations of an array?

An array is a collection of items of the same data type. That means, in an integer array only integer values can be stored, while in a float array only floating values and character array can have only characters. Thus, no array can have values of two data types.

8. What are the advantages of an array?

There are multiple advantages of array data structure and some of them are:

  • Arrays allow random access to elements. This makes accessing elements by position faster.
  • Arrays store multiple data of similar types with the same name.
  • Array data structures are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc.

9. What is the purpose of using arrays?

An array is used when several variables of the same type need to be used, and it can be defined as a sequence of objects of the same type.  

10. What is a multidimensional array?

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in Multidimensional Arrays are stored in row-major order. 




Previous Article
Next Article

Similar Reads

Getting Started with MATLAB
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984. It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorithms and creation of user interfaces. It is both a p
3 min read
Getting Started with Competitive Programming in Python
Python is a great option for programming in Competitive Programming. First off, its easy-to-understand and concise grammar enables quicker development and simpler debugging. The huge standard library of Python offers a wide range of modules and functions that can be used to effectively address programming difficulties. Python also offers dynamic ty
11 min read
Static Data Structure vs Dynamic Data Structure
Data structure is a way of storing and organizing data efficiently such that the required operations on them can be performed be efficient with respect to time as well as memory. Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code. Data structures can be two types : 1. Static Data Structure 2. Dynamic Data
4 min read
Is array a Data Type or Data Structure?
What is Data Type? In computer programming, a data type is a classification of data that determines the type of values that can be stored, manipulated, and processed. It tells the computer what kind of data a particular variable or constant can hold, and what operations can be performed on that data. Common data types include integers, floating-poi
8 min read
Data Structure Alignment : How data is arranged and accessed in Computer Memory?
Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment. Data alignment: Data alignment means putting the data in memory at an address equal to some multiple of the word size.
4 min read
Difference between data type and data structure
Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the compiler where the programmer informs the compile
4 min read
10 Best Tips to Get Started with Codeforces
It is needless to say the importance of competitive programming in any Software Engineer's journey. Most of the beginners have no idea how to use Codeforces and end up wasting a lot of time on it. Most of them also get demotivated when they are not able to solve problems and end up with the thought that they are not capable of doing it. Codeforces
4 min read
How to get started with Competitive Programming in JavaScript
Competitive programming is a popular activity among programmers looking to enhance their problem-solving skills and algorithmic thinking. JavaScript is a wonderful language for competitive programming because of how simple it is to use, how powerful its libraries are, and how quickly it can prototype ideas. JavaScript is a great option for programm
5 min read
How to get started with DSA for FrontEnd Developer Interview?
Data Structures and Algorithms are fundamentals of Programming and Computer Science. One Should have Fundamental knowledge of some basic Data structures and Algorithms and Problem-solving when preparing for a FrontEnd Developer Interview. A foundational knowledge of DSA means understanding the fundamental concepts and principles of Data Structures
6 min read
How to Get Started with Competitive Programming?
If you're a Computer Science student or a coding enthusiast, chances are more you've heard individuals discussing their Competitive Programming skills and rankings & achievements in various coding challenges or contests. And truly, Competitive Programming skills are one of those few skills that set you apart from the crowd, add more value to yo
8 min read
How to get started with DSA for BackEnd Developer Interview?
Data Structures and Algorithms are fundamentals of Programming and Computer Science. One Should have Fundamental knowledge of some basic Data structures and Algorithms Problem-Solving when preparing for a Backend Developer Interview. A foundational knowledge of DSA means understanding the fundamental concepts and principles of Data Structures and A
6 min read
Remove duplicates from unsorted array using Map data structure
Given an unsorted array of integers, print the array after removing the duplicate elements from it. We need to print distinct array elements according to their first occurrence. Examples: Input : arr[] = { 1, 2, 5, 1, 7, 2, 4, 2} Output : 1 2 5 7 4 Explanation : {1, 2} appear more than one time. Approach : Take a hash map, which will store all the
4 min read
Remove duplicates from unsorted array using Set data structure
Given an unsorted array of integers, print the array after removing the duplicate elements from it. We need to print distinct array elements according to their first occurrence. Examples: Input: arr[] = { 1, 2, 5, 1, 7, 2, 4, 2} Output: 1 2 5 7 4 Explanation: {1, 2} appear more than one time. Input: arr[] = { 3, 3, 4, 1, 1} Output: 3 4 1 Approach:
3 min read
Differences between Array and Dictionary Data Structure
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
5 min read
Array Data Structure
Complete Guide to ArraysLearn more about Array in DSA Self Paced CoursePractice Problems on ArraysTop Quizzes on Arrays What is Array?An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding
3 min read
Array Data Structure Guide
An array data structure is a fundamental concept in computer science that stores a collection of elements in a contiguous block of memory. It allows for efficient access to elements using indices and is widely used in programming for organizing and manipulating data. Table of Content What is an Array? Need of Array Data Structures Types of Array Da
5 min read
Introduction to Matrix or Grid Data Structure - Two Dimensional Array
Matrix or Grid is a two-dimensional array mostly used in mathematical and scientific calculations. It is also considered as an array of arrays, where array at each index has the same size. In this article, we will cover all the basics of Matrix, the Operations on Matrix, its implementation, advantages, disadvantages which will help you solve all th
14 min read
Array of Structures vs Array within a Structure in C
Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes. Array within a Structure A structure is a data type in C that allows a group of related variables to be treated as a single unit instead of separate entities. A structure may contain element
5 min read
Applications of Queue Data Structure
Introduction : A queue is a linear data structure that follows the "first-in, first-out" (FIFO) principle. It is a collection of elements that supports two primary operations - enqueue and dequeue. In the enqueue operation, an element is added to the back of the queue, while in the dequeue operation, an element is removed from the front of the queu
5 min read
Data Structure for a single resource reservations
Design a data structure to do reservations of future jobs on a single machine under following constraints. Every job requires exactly k time units of the machine. The machine can do only one job at a time. Time is part of the system. Future Jobs keep coming at different times. Reservation of a future job is done only if there is no existing reserva
7 min read
A data structure for n elements and O(1) operations
Propose a data structure for the following: The data structure would hold elements from 0 to n-1. There is no order on the elements (no ascending/descending order requirement) The complexity of the operations should be as follows: Insertion of an element – O(1) Deletion of an element – O(1) Finding an element – O(1) We strongly recommend to minimiz
4 min read
Tango Tree Data Structure
INTRODUCTION:' Tango Tree is a data structure for efficient dynamic connectivity and range minimum/maximum query on a set of elements. It is a type of balanced binary search tree that uses finger trees as the underlying data structure to achieve fast and efficient operations. The Tango Tree is designed to support both fast insertions and deletions
4 min read
Applications of Graph Data Structure
A graph is a non-linear data structure, which consists of vertices(or nodes) connected by edges(or arcs) where edges may be directed or undirected. In Computer science graphs are used to represent the flow of computation.Google maps uses graphs for building transportation systems, where intersection of two(or more) roads are considered to be a vert
3 min read
Which data structure is used by Map?
What is a Map? Before learning the data structure used by a map, let us have an overlook of map. Map is the part of the STL library that stores key value pairs in it and no two values have the same keys but the different keys can store similar values. The map stores keys in sorted order. These are some functions which map uses with their Time Compl
2 min read
Applications of linked list data structure
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image: Applications of linked list in computer science:Implementation of stacks and queuesImplementation of graphs: Adjacency list representation of graphs is th
5 min read
Top 50 Problems on Heap Data Structure asked in SDE Interviews
A Heap is a special Tree-based Data Structure in which the tree is a complete binary tree. Generally, heaps are of two types: Max-Heap and Min-Heap. To know more about this Data Structure in-depth refer to the Tutorial on Heap Data-Structure. Given below are the most frequently asked interview questions on Heaps:  Easy Interview Questions on Heap D
2 min read
Map Policy Based Data Structure in g++
There are some data structures that are supported by g++ compiler and are not a part of the C++ standard library. One of these is: Policy-Based Data Structure, which is used for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std. This can also be used as a map, to store key and a value (pair) in a
3 min read
Trie Data Structure using smart pointer and OOP in C++
We will implement trie using smart pointers in C++ and OOP. Here, We have already discussed the implementation of trie data using recursionIn our implementation node of a trie look like : C/C++ Code class TrieNode{ public: // Use of shared_ptr for storing Children // Pointers of TrieNode shared_ptr children[ALPHABET_SIZE]; // Tracks whether If this
6 min read
Count-Min Sketch Data Structure with Implementation
The Count-Min Sketch is a probabilistic data structure and is defined as a simple technique to summarize large amounts of frequency data. Count-min sketch algorithm talks about keeping track of the count of things. i.e, How many times an element is present in the set. What is Count-Min Sketch?Count-min sketch approach was proposed by Graham Cormode
7 min read
Introduction to the Probabilistic Data Structure
Introduction: Probabilistic Data Structures are data structures that provide approximate answers to queries about a large dataset, rather than exact answers. These data structures are designed to handle large amounts of data in real-time, by making trade-offs between accuracy and time and space efficiency. Some common examples of Probabilistic Data
5 min read
Practice Tags :