Logo

Programming-Idioms

History of Idiom 118 > diff from v24 to v25

Edit summary for version 25 by Bug38:
[Lua] added input

Version 24

2017-08-21, 12:40:01

Version 25

2017-08-21, 12:41:36

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Illustration

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Illustration
Code
local hash = {}
local res = {}
for _,v in ipairs(test) do
   if (not hash[v]) then
       res[#res+1] = v
       hash[v] = true
   end
end
Code
local x = {'a', 'b', 'c', 'b'}
local hash = {}
local y = {}
for _,v in ipairs(x) do
   if (not hash[v]) then
       y[#y+1] = v
       hash[v] = true
   end
end
Doc URL
https://stackoverflow.com/a/20067270
Doc URL
https://stackoverflow.com/a/20067270