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

-- returns a `counts` map such that `counts[value]`
-- is the number of times that `value` appears in `list`
function countMap(list)
    local counts = {}
    for _, value in ipairs(list) do
        if counts[value] then   
            -- 2nd or on time that we've seen this element
            counts[value] = counts[value] + 1
        else
            -- 1st time we've seen this element
            counts[value] = 1
        end
    end
    return counts
end

This can be shortened a bit, if you want:

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

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

-- returns a `key` such that `map[key]` is as large as possible
function keyLargest(map)
    local best = nil
    for key in pairs(map) do
        if best == nil then
            -- first key in the map
            best = key
        elseif map[best] < map[key] then
            -- `key` has a bigger value than `best`,
            -- so `key` is our new best
            best = key
        end
    end
    return best
end

This can be shortened a bit, if you want:

-- returns a `key` such that `map[key]` is as large as possible
function keyLargest(map)
    local best = next(map) -- get an arbitrary key
    for key in pairs(map) do
        if map[best] < map[key] then
            best = key
        end
    end
    return best
end

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

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

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