# How to get the majority

if there were 10,000 parts, and they all had an integervalue each of them are randomly assigned a
number 1 through 1000 How could i get them all and see which number is the most popular among parts

I wrote this function for it along with an explanation of what the function does. Let me know if it doesn’t work or if anythings confusing:

THE FUNCTION ASSUMES THAT THE INT VALUE IS NAMED ‘IntValue’

If this is not the case, change IntValue in the script to be whatever the intvalue is named.
if you dont know the name of the intvalue, you can use :FindFirstChildWhichIsA(“IntValue”)

``````--[[
EXPLANATION OF EACH VARIABLE:

tableContainingParts - the table of 10000 parts each part containing an int value

partsSortedByNumValue - a dictonary containing the 10000 parts sorted by their IntValue
EXAMPLE:
if i had the following parts with specified intvalues:
part:	intvalue:
part1	100
part2: 	100
part3: 	50
part4: 	100
part5: 	60

partsSortedByNumValue would end up looking like this:
partsSortedByNumValue = {
[50] = {part3};
[60] = {part5};
[100] = {part1, part2, part4};
}

majorityIntValue = the int value that most parts contain
numMajorityParts - the number of parts with the majority int value
]]

-- the function goes through tableContainingParts, adds the part to partsSortedByNumValue with it's key being the part's int value
-- after the part is added, it checks if the key currently has the highest amount of intvalues if it does, it replaces majorityIntValue/numMajorityParts
--function returns the majority int value and a table containing each part with that int value
``````
``````function getMostPopularIntValue(tableContainingParts)
local partsSortedByNumValue = {}

local numMajorityParts = 0
local majorityIntValue = nil

for _, part in pairs(tableContainingParts)do
partsSortedByNumValue[part.IntValue.Value] = partsSortedByNumValue[part.IntValue.Value] or {}
table.insert(partsSortedByNumValue[part.IntValue.Value], part)

if #partsSortedByNumValue[part.IntValue.Value] > numMajorityParts then
majorityIntValue = part.IntValue.Value
numMajorityParts = #partsSortedByNumValue[part.IntValue.Value]
end
end

return majorityIntValue, partsSortedByNumValue[majorityIntValue]
end

local intValue, partsWithIntValue = getMostPopularIntValue(workspace.Parts:GetChildren())``````