lua-判断字典里哪个元素占得比例最大

--输入n个数
--[[
4
blue
blue
red
blue

output:blue
]]





--输入一个数
n= io.read()
--循环输入n个字符串,将其记录到table中
dic= {}
for i = 1,n do
str = io.read()
--如果字符串已经在table中存在了
if    dic[str] then
dic[str]=dic[str]+1
else
dic[str]=1
end
end
maxV= -1
result= ""
for k,v in pairs(dic) do
if v>maxV then
maxV= v
result = k
end
end
print(result)



posted @ 2016-10-27 21:01  无畏先锋  阅读(539)  评论(0编辑  收藏  举报