0

What time complexity does the Lua function: table.find(t, v) have?

I am unable to find a solution from open source.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mini-Yip
  • 1
  • 1

1 Answers1

4

Lua does not have a table.find. Many utility libraries implement a table.find though. Roblox's Luau has a table.find as well.

All of these have in common that the table t is a list - that is, the keys are subsequent integers >= 1 - and the elements must not be sorted. This means table.find can not use a binary search (O(log n)) or hash lookup (O(1)) but must use a linear search in O(n) average & worst case time.

If you want the O(1) hash map performance, use your keys as table keys rather than inserting them into a list. You can keep both a list and a hash map if order matters.

Luatic
  • 8,513
  • 2
  • 13
  • 34