我在lua有一张桌子
test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}
我想删除表中的所有重复元素.
输出应该是
test = {1,2,4,3,"A","B"}
我的尝试:
> items = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}
> flags = {}
> for i=1,table.getn(items) do
if not flags[items[i]] then
io.write(' ' .. items[i])
flags[items[i]] = true
end
>> end
1 2 4 3 A B>
现在工作正常.感谢您的回答和评论.
vog*_*tix 19
类似于@Dimitry给出的示例,但只有一个循环
local test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}
local hash = {}
local res = {}
for _,v in ipairs(test) do
if (not hash[v]) then
res[#res+1] = v -- you could print here instead of saving to result table if you wanted
hash[v] = true
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9696 次 |
| 最近记录: |