Learn Lua · Tables · Lesson 5 of 5
Sets
A set holds each value once. set.new builds one from a list, dropping repeats, and
set.tolist gives the members back as a list, in the order they were first added:
local colours = set.new({ "cyan", "black", "cyan" })
set.has(colours, "black") --> true
set.tolist(colours) --> {"cyan", "black"}set.union(a, b) has everything in either set, and set.intersection(a, b) only what
is in both.
What your code receives
Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.
input = {
colours = { "cyan", "black", "cyan", "magenta" },
front = { "cyan", "black" },
back = { "black" },
}Steps
- Return
unique: the colours ininput.colourswithout repeats, in the order first seen. - Add
hasBlack: whether"black"is one of them. - Add
both: the colours used on bothinput.frontandinput.back, as a list.
Show a hint
Make sets with set.new, then use set.has, set.intersection and set.tolist.