Last active
April 20, 2020 08:23
-
-
Save Dobby233Liu/8ea63af56400892f2d98fc9e21ed7aed to your computer and use it in GitHub Desktop.
ineffective way to dedup a table in lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- deduplicate value-only table | |
function dedupTable(origtable) | |
function tableContainsV(tablee, value) | |
local fr = false | |
for i = 1, #tablee do | |
if tablee[i] == value then | |
fr = true | |
break -- i had breakfast today | |
end | |
end | |
return fr | |
end | |
local messtable = {} | |
local ie = 0 | |
for i = 1, #origtable do | |
if not tableContainsV(messtable, origtable[i]) then | |
ie = ie + 1 | |
messtable[ie] = origtable[i] | |
-- else DEBUG("dupe!!! origtable["..i.."]") | |
end | |
end | |
return messtable | |
end | |
function _DeDupTool_consoleDemo() | |
print("{ "..table.concat(dedupTable({1,1,2,8,8,8,8,9}),",").." }") | |
end | |
_DeDupTool_consoleDemo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment