How to fetch the amount of a certain item in a table

For example, if i were to have a table like this

local exampletable = ["Apple","Apple","Banana"]

How would i get the amount of “Apple” in that table (which would obviosuly equal to 2), instead of running through everything in the table, and having “Banana” count aswell?
I know there are things like the # operator, but that would simply end up counting EVERYTHING- including the banana.

You can implement a custom counting iterator with a for loop:

function count<T>(needle: T, haystack: { T }): number
	local count = 0;
	
	for _, straw: T in haystack do
		if straw == needle then count += 1; end
	end
	
	return count;
end
1 Like