Array

A homogeneous collection of elements that can be accessed using an index. It is fixed-size, typically contagious and is the most compact collection with no extra overhead per element.

Pros Cons
Most compact collection Fixed-size
Indexable Supports only one data type per array
Implementations
C++: Array

std::array<int, 3> arr = {1, 2, 3};

Java: Array

int[] arr = new int[3];
int[0] = 1;

Python: Array

from array import array
arr = array('l', [1, 2, 3])
arr[0] = 0

C++: C-style array

int arr[3] = {1, 2, 3}
arr[0] = 0;

Operations Worst Average
IndexO(1)O(1)
LeetCode Problems
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Number of Islands
Given an m x n 2D binary grid which represents a map of '1's (land) and '0's (water), return the number of islands.

Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.


Similar