Menu
Coddy logo textTech

Recap: Arrays and Maps

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 21 of 73.

challenge icon

Challenge

Medium

Create a typed array items: {string} initialized with:

{"apple", "banana", "apple", "cherry", "banana", "apple"}

Build a typed map counts: {[string]: number} by looping over items with ipairs and counting how many times each item appears.

Then print one line per distinct item in alphabetical order, formatted exactly like apple: 3. To get a stable order, collect the map's keys into a {string} array, sort it with table.sort, and loop over the sorted keys with ipairs.

Try it yourself

-- Write code here
-- 1) items: {string} with the six fruits
-- 2) tally into counts: {[string]: number} using ipairs
-- 3) collect keys into a {string} array, table.sort it
-- 4) print `{name}: {count}` for each key in sorted order

All lessons in Introduction To Luau