For the edited question, you can store the table items in a set with their count like this:
mvp = {"player1", "player2", "player3", "player1", "player2", "player1", "player2", "player3", "player1"}
local t = {}
for k, v in ipairs(mvp) do
t[v] = (t[v] or 0) + 1
end
local count = {}
local index = 1
for k, v in pairs(t) do
count[index] = k .. " - " .. v
index = index + 1
end
str = table.concat(count, ", ")
print(str)
Output: player1 - 4, player3 - 2, player2 - 3
Note that the names are not sorted, because of the usage of pairs()
, you need some extra work to do if the names need to be sorted.