Hello, so I was practicing with tables, numbers, and find the mode of a data set. I was looking to see how I could make this code shorter and more efficient. If there is, I don’t want something that can make it simple like table.___
I was looking at this and thought it could use a lot of improvement but didn’t know-how. Thanks!
local DataSet = {1,2,3,4,5,1,2,3,4}
local Amount = {}
local f = {}
local ab = 0
local AllSame = true;
local mode = ""
for i,v in pairs(DataSet) do
if not Amount[tostring(v)]then
Amount[tostring(v)] = 1
else
Amount[tostring(v)] = Amount[tostring(v)] + 1
end
end
--end of amount set
for i,v in pairs(Amount) do
if tonumber(v)>ab then
ab=tonumber(v)
end
--warn(i,"..",v)
end
--end of top number
for i,v in pairs(Amount) do
if tonumber(v)==ab then
table.insert(f,tonumber(i))
end
end
--end of adding all top numbers
for i,v in pairs(Amount) do
if v<ab then
AllSame = false
end
end
--end of finding if all numbers are the same
if not AllSame then
for i,v in pairs(f)do
if #mode == 0 then
mode = mode..tostring(v)
else
mode = mode..","..tostring(v)
end
end
warn("The Mode of the data set is {"..mode.."}")
else
warn("There is no mode")
end
--Putting it together