Back to Blog

This is bananas: why you don't get arrays

Finding the avocado 🥑 in an array illustrates a core computer science problem: search. Arrays don't remember storage locations — you provide items and they forget where things went.

A peeled banana curled into the shape of a question mark

The array is among the first data structures you meet when you come in contact with coding. Relatively easy to wrap your head around on the outside, it gets trickier on the inside — where methods like filter, map, or find do their work. Let's see why.

Take the find method, and the following array.

const fruits = [🍑, 🍌, 🥑, 🍊, 🍉];

I'm looking for the avocado 🥑. Help me find it.

Sure — you can just look at the array, point at index 2, and be right: fruits[2]. With some seasoning, you'd enjoy your avocado.

But what if you couldn't see where the avocado is?

const fruits = [🙈, 🙈, 🙈, 🙈, 🙈]

The same fruits are still in the array, but now you're blindfolded and don't get to play peek-a-boo.

I'm still hungry 😋 — go find my avocado 🥑!

const avocado = fruits.find((fruit) => fruit === 🥑);

All right, that was fast. Let's talk about it.

Finding the avocado 🥑 is the core problem in all of computer science

Think of the hard problems in computing, and all sorts of fascinating challenges come to mind — except the one I'm about to talk about.

When I was a student, the professor told us that the single most important problem in computer science was — search.

And I was in disbelief.

He was a funny professor. I figured he was joking. Search couldn't possibly be the most important problem. No way.

Well.

What you need to know about arrays — and what my younger self didn't yet know — is that the computer has no clue where it stores anything 😶.

You: Hey Array, take my avocado 🥑. Thanks.

Instant amnesia. The array doesn't even remember what it just stored.

You: Erhm, Array, where's my 🥑?

Array: Don't know. Go search for it.

That's how arrays work.

Retrieving anything means walking the list one item at a time and looking at each one to see 🐵 what you're holding.

The array doesn't make a note of where it puts anything. You might know — you pushed the pineapple 🍍 yourself, so it's the last item at the very end. You know that. But your knowledge is external to the array; it's not part of the array.

All the array knows is how many entries it holds. Anything more specific than that, you search for.

So, did you find the 🥑?

Keep it. It's yours.