generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How to find the mode in a table?

Previous Thread :: Next Thread 
denfeet is online. denfeet
Joined: 07 Oct 2008
Total Posts: 57
06 Mar 2017 05:31 PM
How would I find the mode within a table or array in a script.

For example:

votes = {"denfeet", "denfeet", "somerandomnoob"}

If i had a bunch of player names in this voting array, then how can I find the name that was voted for the most out of everyone?
Report Abuse
alij12 is not online. alij12
Joined: 03 Oct 2011
Total Posts: 1204
06 Mar 2017 05:33 PM
Im assuming you're asking how to search an array. Unless your game has an absurd amount of players, the easiest way is to use a linear search:

function findInTable(plr,table)
for i = 1, table.length do --My syntax could be wrong here
if table[i] == plr then
return i;
end
end
return -1;
end


#code Do not give up; the beginning is always the hardest part.
Report Abuse
denfeet is online. denfeet
Joined: 07 Oct 2008
Total Posts: 57
06 Mar 2017 05:41 PM
I don't just want to search it, I want to find the value that appears the most
Report Abuse
alij12 is not online. alij12
Joined: 03 Oct 2011
Total Posts: 1204
06 Mar 2017 06:05 PM
There might be a more efficient way, but this is what I came up with:

local plrs = {"plr123","idk","o","roblox","shedletsky","shedletsky"}
local votes = {}


function findMostInTable(array)
local temp = plrs
for i = 1,#temp do
votes[i] = 1
end
for i,v in pairs(temp) do
if i > 1 then
for j = 1,i - 1 do
if temp[j] == v then
temp[i] = nil
votes[j] = votes[j] + 1
end
end
end
end
local most = -1
local index = -1
for i,v in pairs(votes) do
if v > most then
most = v
index = i
end
end
return plrs[index]
end

print(findMostInTable(plrs))


#code Do not give up; the beginning is always the hardest part.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image