Reputation: 77
How to remove duplicate ids from table?
local xx={{id=1,name=user1},{id=1,name=user1},{id=1,name=user1},{id=3,name=user3},{id=2,name=user2}}
local result = {}
for key,value in ipairs(xx) do
if value.id ~=xx[key+1] then
table.insert(result,value)
end
end
for key,value in ipairs(result) do
print(key,value.id)
end
I want to print like this 1,3,2.
Upvotes: 0
Views: 409
Reputation: 5031
you can take the value name
and use it as a key, to create a duplicate free unordered table.
local t={{id=1,name='a'},{id=1,name='a'},{id=1,name='a'},{id=3,name='c'},{id=2,name='b'}}
local users = {}
for k, v in ipairs(t) do
users[v.name] = v.id
end
for k, v in pairs(users) do
print(k,v)
end
Output
b 2
a 1
c 3
Upvotes: 1