← Lua Academy

Practical scripting · 66 / 69

Keep unique identifiers

YOUR TASK

Make this happen

3 steps
  1. Use a set-like map to remember identifiers already seen. Return each identifier once, preserving the order of its first appearance.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

exports.unique = function(values: { string }): { string }
  return values
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("keeps first occurrence", function()
  local s = subject.load()
  t.expect(s.unique({ "b", "a", "b", "c", "a" })):deep_eq({ "b", "a", "c" })
end)
t.it("empty input", function()
  local s = subject.load()
  t.expect(#s.unique({})):eq(0)
end)