Module:Util/tables/unique

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

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

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