The count property returns the total number of elements present in the set.
Example
count Syntax
The syntax of the set count property is:
set.count
Here, set is an object of the Set class.
count Return Values
The count property returns the total number of elements present in the set.
Example 1: Swift Set count
Output
3 0
In the above example, since
- names contain three string elements, the property returns 3.
- employees is an empty set, the property returns 0.
Example 2: Using count With if...else
Output
The set size is large
In the above example, we have created the set named numbers with 10 elements.
Here, since there are 10 elements in the set, numbers.count > 5 evaluates to true, so the statement inside the if block is executed.