Recap: Arrays and Maps
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 21 of 73.
Challenge
MediumCreate 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
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps