Data Structure 1: Arrays 🗂️
We will be looking at what an Array is, and what it is used for.
  • Data Structure
September 10, 2024
2 min read

Introduction

Arrays are one of the most fundamental data structures in computer science. They provide a simple and efficient way to store and manage collections of data. In this blog post, we'll explore arrays in detail, including their operations, advantages, limitations, and things to be mindful of when working with them.

What is an Array?

An array is a collection of elements stored in contiguous memory locations. Each element in an array is identified by an index, which starts at 0 in most programming languages. Arrays are widely used due to their simplicity and the constant-time access to elements using their index.

Characteristics of Arrays

  1. Fixed size: Once an array is created, its size cannot be changed.
  2. Homogeneous data: Arrays typically store elements of the same data type.
  3. Random access: You can access any element directly using its index.

Common Array Operations

  1. Insertion
    Adding an element to an array can be tricky because arrays have a fixed size. If the array has unused space: At the end: This is the simplest case and has a time complexity of O(1). At a specific index: Requires shifting elements to make room, with a time complexity of O(n) in the worst case.

  1. Deletion
    Removing an element from an array involves shifting the remaining elements to fill the gap. From the end: The easiest case, with a time complexity of O(1). From a specific index: Requires shifting elements and has a time complexity of O(n).

  1. Search
    Finding an element in an array can be done in two main ways:
    Linear Search: Checks each element one by one (time complexity O(n)).
    Binary Search: Requires the array to be sorted and has a time complexity of O(log n).

Things to be careful about: For binary search, ensure the array is sorted before searching. Handle edge cases like empty arrays or elements not found.


  1. Traversal
    Iterating through all elements of the array is common for tasks like printing, summing, or processing data. Traversal has a time complexity of O(n).

Best Practices When Using Arrays

Two-pointer technique is where we iterate over the Array in two different places at the same time.

imageImage sourced from LeetCode