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