Module:Util/tables/unique
< Module:Util | tables
unique(array)
Returns
- A copy of
array
but without the duplicate values. Elements are deep-compared. he order of result values is determined by the order they occur in the array.
Examples
# | Input | Output | Result |
---|---|---|---|
1 | unique({
1,
2,
2,
{ foo = "bar" },
{ foo = "quux" },
{ foo = "bar" },
})
| {
1,
2,
{ foo = "bar" },
{ foo = "quux" },
}
|
local _isEqual = require("Module:Util/tables/_isEqual")
local find = require("Module:Util/tables/find")
local function unique(tbl)
local result = {}
local seen = {}
for i, v in ipairs(tbl) do
if type(v) ~= "table" and not seen[v] then
seen[v] = true
table.insert(result, v)
elseif not find(result, _isEqual(v)) then
table.insert(result, v)
end
end
return result
end
return unique