Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you find out which value in a table has occurred the most?

So, I'm adding values to my table, right now only numbers, and I was wondering how you can find out which value has occurred in the table the most. Like lets say that in the table there are 2 values of 12, and one value of 1, is there a way to find out which value occurred the most, in this case 12? this is what i have so far

Numbers = {}

for i = 1,5 do 
	number = math.random(10)
	for i,v in pairs(Numbers) do 
		if Numbers[i] == number	 then
			print(number,is occurred again)
		end
	end
	table.insert(Numbers,number)
end

1 answer

✓ Accepted answer

You could make a simple frequency table like so.

local numbers = {10, 10, 12, 12, 15, 10}

function Count(num)
	for _, num in pairs(num) do
		if script:FindFirstChild(num) then
			script[num].Value = script[num].Value + 1
		else
			local new = Instance.new('IntValue', script).Name = num
			new.Value = 1
		end
	end
	local results = {}
	for _, num in pairs(count) do
		results[tostring(num)] = script[num].Value
	end
	return results --Returns a dictionary of all values that have been requested to track. Have not been tested.
end

print(Count(numbers)[10])

This would print "Ten : 3, Twelve : 2".

Also, math.random needs rather 0 parameters or 2, not 1. You can do

local anum = math.random()
print(anum)

and that would print decimal numbers from 0 to 1, or have numbers between two numbers like

local anum = math.random(1, 10)
print(anum)

This would not print decimals, however print a number from 1 to 10. Also, use math.randomseed. It randomizes your math.random statements. To make the most random numbers that is NOT predictable what will happen even if you know what time it is, nor using math.random, I do

math.randomseed(tick() / wait() * wait() % wait() ^ wait() - wait() + wait())

If you dont know already, wait() returns a number how long it actually waited. Sometimes they have to hold off the task because something else is running and they cannot handle running another task at the moment. Even in a plain baseplate game, this will always be different. All those operators do not have to be in order, they are just there to make it random.

Also, math.randomseed needs to be before you do math.random like

math.randomseed(tick() / wait() * wait() % wait() ^ wait() - wait() + wait())
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)
print(math.random(1, 10)

Hope this helps!

4 comments

0

Thanks, this sort of helps, but I there are going to be thousands of different values, is there a way to do it for thousands of values, or is that impossible?

0

Try my soultion.

0

I know it works, but I'm wondering how it would work for like thousands of numbers, would i have to post all of them in the table myself? or is there another way? thousands of different combinations I mean

0

There.

This discussion is preserved as it was. New answers, comments, and votes are closed.