Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How do I detect the most common value in a table?

Asked by 8 years ago
Edited 8 years ago

For Context: I am making a raid system for a clan that detects a common group between all the raiders.

My Procedure: Basically, I use GroupService to get all the groups that each player in-game is in; then I add the group names to a table.

In order to predict which group is raiding, I want to find which string (the group name) is the most common in the table. In essence, the mode of the data.

My question is, what function would I use to figure out the mode? Is there a default ROBLOX function for this? If not, how can I make one myself?

0
You can either periodically count the amount of each group using a hashtable, or just add/subtract to a table when a player of a certain group joins/leaves. If you would like more details (or an example), let me know. Tkdriverx 514 — 8y

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

There is no built-in function to do this (the Lua standard library is very bare, and ROBLOX doesn't really add anything that deals with plain types like tables)

Here's an outline of how you might do it:

Given your list, build a map count where count[value] is the number of times that value appeared in list.

Implementation

01-- returns a `counts` map such that `counts[value]`
02-- is the number of times that `value` appears in `list`
03function countMap(list)
04    local counts = {}
05    for _, value in ipairs(list) do
06        if counts[value] then  
07            -- 2nd or on time that we've seen this element
08            counts[value] = counts[value] + 1
09        else
10            -- 1st time we've seen this element
11            counts[value] = 1
12        end
13    end
14    return counts
15end

This can be shortened a bit, if you want:

01-- returns a `counts` map such that `counts[value]`
02-- is the number of times that `value` appears in `list`
03function countMap(list)
04    local counts = {}
05    for _, value in ipairs(list) do
06        -- "default" counts[value] to 0 if it's not already there
07        counts[value] = (counts[value] or 0) + 1
08    end
09    return counts
10end

After we have the counts, we just need to find the key that has the largest value.

01-- returns a `key` such that `map[key]` is as large as possible
02function keyLargest(map)
03    local best = nil
04    for key in pairs(map) do
05        if best == nil then
06            -- first key in the map
07            best = key
08        elseif map[best] < map[key] then
09            -- `key` has a bigger value than `best`,
10            -- so `key` is our new best
11            best = key
12        end
13    end
14    return best
15end

This can be shortened a bit, if you want:

01-- returns a `key` such that `map[key]` is as large as possible
02function keyLargest(map)
03    local best = next(map) -- get an arbitrary key
04    for key in pairs(map) do
05        if map[best] < map[key] then
06            best = key
07        end
08    end
09    return best
10end

Then mostCommon can just be defined in terms of the above two functions:

1-- returns an element that appears the most number of times in `list`
2function mostCommon(list)
3    return keyLargest(countMap(list))
4end

Note that this will break ties arbitrarily, i.e., mostCommon{1, 1, 2, 2} could be either 1 or 2.

0
Grr! I was about to answer. xD Tkdriverx 514 — 8y
0
Thank you both of you. :) ShadowsDev 62 — 8y
Ad

Answer this question