Hash Tables
Frequency Count
Walk a sequence and count occurrences of each value in a map. Classic "get current count, add one, write back" loop.
Algorithm
Canonical input
{"fig", "apple", "fig", "pear", "apple", "fig"} produces the final
map {fig: 3, apple: 2, pear: 1}.
Basic Implementation
basic.lua
local words = {"fig", "apple", "fig", "pear", "apple", "fig"}
local counts = {}
local order = {}
local i = 1
while i <= #words do
local word = words[i]
if counts[word] == nil then
order[#order + 1] = word
counts[word] = 1
else
local prev = counts[word]
counts[word] = prev + 1
end
i = i + 1
end
io.write("{")
local j = 1
while j <= #order do
if j > 1 then
io.write(", ")
end
local key = order[j]
io.write(key .. ": " .. tostring(counts[key]))
j = j + 1
end
io.write("}\n")
Complexity
- Time: O(n) average with Lua tables (hash-table backed for string keys).
- Space: O(k) where k is the number of distinct keys.
Implementation notes
- Lua:
counts = {}is the idiomatic table; thecounts[word] == nilpredicate plus an explicit assignment keeps the lesson on the read-or-default path without hiding it behind a metatable__indexdefault. Lua has noarray_count_valuesshortcut, so the explicit loop already mirrors the lesson spec. - The auxiliary
orderbuffer makes the first-seen order explicit so the final printout does not lean on Lua's unspecified hash-key iteration order as a contract. - The replay renders the map as a list of key/value rows in first-seen order and animates the count increment on each frame.
get-or-default
A first-time `word` triggers the "default" branch: append to `order` and set `counts[word] = 1`. A repeat read-modify-writes `counts[word] = prev + 1`.
first-seen order
Keys are tracked in `order` (a plain sequence) to keep the printout deterministic; Lua's table iteration order is unspecified for non-sequence keys.