Dynamic array

One of the most commonly used data structures, it is a growable/shrinkable contiguous list of elements.

Pros Cons
Has similar performance for array but is still growable If it has to grow, it will take O(n) time to create a larger array and copy the data over
Fast random access/indexing to elements Non-optimal performance when inserting/deleting from middle of array
Contiguous elements lead to good locality of reference and cach
Implementations
Java: ArrayList

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

Python: List

collection = [1, 2, 3]
collection.append(4)

C++: Vector

std::vector<int> v = { 1, 2, 3};

Operations Worst Average
Delete (Beginning/Middle)O(n)
Delete (End)O(1)
IndexO(1)
Insert (Beginning/Middle)O(n)
Insert (End)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