When is an array the right choice? | IntuitionRewire
When is an array the right choice?
You now know why arrays exist and how to use them. This chapter is about when: recognising, from a problem statement, that an array is the right tool — and noticing when it isn't.
What arrays are great at
Reading and writing by position in O(1): "the 5th element", "the value at index i", "the cell at row r, column c".
Walking through everything in order, which is also very fast in practice: the elements sit side by side in memory, so the CPU's cache loads them in batches.
Storing data compactly: no pointers or extra bookkeeping per element.
Adding and removing at the end in O(1) (for growable arrays).
Signals in a problem statement
These words and shapes almost always mean the problem is an array problem — and hint at the technique:
"index", "position", "i-th", "k-th" — you'll access elements by position.
"subarray", "contiguous", "window", "consecutive days" — a slice of neighbouring elements: think sliding window, prefix sums, or Kadane's algorithm.
"sorted array" — two pointers or binary search are nearby.
"in place", "O(1) extra space" — rearrange the array itself, usually with read/write or two-ends pointers.
"grid", "matrix", "board", "image" — a 2D array.
Values in a small range ("lowercase letters", "numbers from 1 to n", "0, 1 and 2") — an array can act as a counter indexed by value.
When an array is NOT the best tool
The costs behind that guide:
Search by value: array O(n) (O(log n) if sorted) — hash set O(1) on average.
Insert/delete in the middle: array O(n) because of shifting — linked list O(1) once you're at the spot.
Get the minimum repeatedly while adding items: array O(n) each time — heap O(log n).
Access the i-th element: array O(1) — linked list O(n), because it must walk from the head.
In practice, the answer is often "an array, plus something": an array plus a hash map (Two Sum), an array plus a heap (k largest elements), an array used as a stack. Stacks, queues, heaps and hash tables are themselves usually built on arrays underneath.
The array as a counter or lookup table
One of the most useful "when" moments: if the possible values are small whole numbers, you don't need a hash map at all. An array indexed by the value is faster and simpler.
Counting letters in a word: counts = new Array(26), indexed by letter - 'a'.
Checking which numbers from 1 to n appear: a boolean array of size n + 1.
Sorting values known to be 0, 1 or 2: count them — or use the Dutch national flag in one pass.
A quick self-test
For each description, which would you reach for? (Answers below.)
A) "Return the k-th element of this list" — a hundred million times.
B) "Has this user ID appeared before?" — for a stream of IDs.
C) "The longest run of consecutive days where the temperature rose."
D) "Repeatedly take the cheapest task and add new tasks as they arrive."
Answers: A) an array — O(1) access by position. B) a hash set — O(1) lookup by value. C) an array walked once, carrying the current run length. D) a heap — though it's stored in an array under the hood.