🎓 A1Learner
🔍
0 of 52 lessons completed

Lua Tables as Sets

A set is a collection where each member appears only once. A Lua table works as a set when each member is a key with the value true.

True values mark members

Each member is a key set to true. A lookup gives true for a member and nil for a stranger.

local fruits = {apple = true, banana = true}
print(fruits.apple)
print(fruits.pear)

Union and intersection combine sets

Union keeps a member from either set. Intersection keeps only a member found in both sets.

local warmKeys = {"red", "blue"}
local coolKeys = {"blue", "green"}
local warm = {}
for i = 1, #warmKeys do
  warm[warmKeys[i]] = true
end
local cool = {}
for i = 1, #coolKeys do
  cool[coolKeys[i]] = true
end
local union = {}
for i = 1, #warmKeys do
  union[warmKeys[i]] = true
end
for i = 1, #coolKeys do
  union[coolKeys[i]] = true
end
local both = {}
for i = 1, #warmKeys do
  if cool[warmKeys[i]] then
    both[warmKeys[i]] = true
  end
end
print(union.red, union.blue, union.green)
print(both.blue, both.red)

Difference drops shared members

Difference starts from the first set. It drops any member that also appears in the second set.

local firstKeys = {"red", "blue"}
local secondKeys = {"blue", "green"}
local first = {}
for i = 1, #firstKeys do
  first[firstKeys[i]] = true
end
local second = {}
for i = 1, #secondKeys do
  second[secondKeys[i]] = true
end
local diff = {}
for i = 1, #firstKeys do
  if not second[firstKeys[i]] then
    diff[firstKeys[i]] = true
  end
end
print(diff.red, diff.blue)

Example

local artList = {"mia", "leo", "ana"}
local musicList = {"leo", "ana", "sam"}
local art = {}
for i = 1, #artList do
  art[artList[i]] = true
end
local music = {}
for i = 1, #musicList do
  music[musicList[i]] = true
end
local all = {"mia", "leo", "ana", "sam"}
local unionCount = 0
local bothCount = 0
local artOnlyCount = 0
for i = 1, #all do
  local name = all[i]
  if art[name] or music[name] then
    unionCount = unionCount + 1
  end
  if art[name] and music[name] then
    bothCount = bothCount + 1
  end
  if art[name] and not music[name] then
    artOnlyCount = artOnlyCount + 1
  end
end
print(unionCount, bothCount, artOnlyCount)

Key points

  • Membership: membership is tested by looking up a key, since a missing key gives nil.
  • Add: adding a member means setting its key to true.
  • Remove: removing a member means setting its key to nil.
  • Union: a union holds every member that appears in either set.
  • Intersection: an intersection holds only members that appear in both sets.
  • Difference: a difference holds members of the first set that are missing from the second.

In this example, two clubs are built as sets from member lists. A single pass over every name counts the union, the shared members, and the art-only members. The printed counts show four in either club, two in both, and one in art only.

Note

adding the same key twice still leaves one member, because a table holds each key once.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What value does a lookup give for a key that is not in a set?