I’m programming in Pascal and I’m trying to find if is there any already created function/procedure in the standard library or similar that allows me to know how many elements a set has?
I know how to do it by using a for loop and a counter increasing +1 when it finds an element (similar to what it has to be done when you want to print a set) but I was wondering if is there any other easier way of doing it, like I said, some predefined standard function/procedure?
Not in Standard Pascal, no. Many implementations have a nonstandard addition; for instance, GNU Pascal has the function
Card(short for “cardinality”): http://www.gnu-pascal.de/gpc/Card.html#Card .EDIT I see this is tagged Free Pascal, and according to this page, it lacks such an addition. You have to loop over the set incrementing a counter. At least you only have to loop over the elements actually in the set, rather than looping over all the possibilities and checking to see if each one is present…