Home

Array Data Structure

What is an Array?

An array is a collection of elements identified by index or key, where each element is stored in contiguous memory locations. Arrays provide efficient access to elements using their index and are commonly used in programming for storing collections of data.

Arrays are ideal for scenarios where the size of the data structure is known in advance and where quick access to elements by index is needed.

Real-World Analogy

Think of an array as a row of mailboxes, where each mailbox has a specific number (index). Each mailbox holds a piece of mail (data) and you can quickly access any mailbox if you know its number.

Example

Consider an array that stores the following integers: 10, 20, 30, 40, and 50. The array is structured as follows:

[10, 20, 30, 40, 50]

Each number is stored at a specific index, with 10 at index 0, 20 at index 1, and so on.

Array Visualization

10
20
30
40
50

Code Snippets

C++

int arr[] = {10, 20, 30, 40, 50};

Java

int[] arr = {10, 20, 30, 40, 50};

Python

arr = [10, 20, 30, 40, 50]

C

int arr[] = {10, 20, 30, 40, 50};