Lua : remove duplicate elements
Asked Answered
B

4

13

i am having a table in lua

test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}

I want to remove all duplicate elements in table.
Output should be

test = {1,2,4,3,"A","B"}

EDIT:

My try :

> 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>

It is working fine now. Thanks for answers and comments.

Blackmun answered 19/11, 2013 at 8:43 Comment(1)
note that none of the answers thus far posted handle the case of duplicate elements which are themselves also tables, as that also requires a satisfactory answer to how to compare tablesPopliteal
C
26

Similar to example given by @Dimitry but only one loop

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
Clergy answered 19/11, 2013 at 9:6 Comment(2)
nice shortening. The brackets of the condition are not really necessary, though :DKeramic
@dmitry thanks - the brackets are my normal C, Perl and PHP showing through :-PClergy
K
4
local test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"}

-- make unique keys
local hash = {}
for _,v in ipairs(test) do
    hash[v] = true
end

-- transform keys back into values
local res = {}
for k,_ in pairs(hash) do
    res[#res+1] = k
end

-- 1 2 3 4 A B
for _,v in ipairs(res) do
    print(v)
end

test = res

... a simple, straightforward solution just from the head, but I think, the hint is given in the PiL book

what have you tried to solve the problem?

Keramic answered 19/11, 2013 at 8:49 Comment(5)
please see i edited my question. thanks for your answer . will you please explain why you _ ??Blackmun
I've edited the title so that it's readable as plain text. The underscores don't convey extra information. Your solution is in principle the same as mine now, just that you're not writing back to the test table handle. Please consider accepting the answer if you agree, it solves your problemKeramic
sure i will accept i am just doing same research to solve this problem with very less codeBlackmun
res[#res+1] = k what # is doing here ? please explain ?Blackmun
Its finding the last index in the table for Lua 5.1 onClergy
I
3
local xx = {'a','b','c','d','a','d','f','g','a'}
table.sort(xx)
local result = {}

for key,value in ipairs(xx) do
  if value ~=xx[key+1] then
    table.insert(result,value)
  end
end

for key,value in ipairs(result) do
  print(key,value)   
end
Idonah answered 28/6, 2018 at 22:47 Comment(1)
Usually, even if this code answers the question, it is recommended to briefly explain what it does and why it is the right answer.Montague
B
0
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

Output -

1 2 4 3 A B>
Blackmun answered 19/11, 2013 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.