Swift Set Size Using the count Property
To get the size of a Set in Swift, access its count property. The returned integer is the number of unique elements currently stored in the Set.
Swift Sets do not store duplicate values. Therefore, count reports the number of distinct elements rather than the number of values originally supplied during initialization.
Swift Set count Syntax
The syntax to access count property of the Set set is
set.count
The count property returns an Int. It can be read from a Set declared with either let or var.
Get the Size of an Integer Set in Swift
In the following example, we initialize a Set of integers, and find the size of this Set using count property.
main.swift
var numbers: Set = [25,12,74,16,18]
var size = numbers.count
print("Size of Set is: \(size)")
Output
Size of Set is: 5
The Set contains five distinct integers, so numbers.count returns 5.
Count the Elements in a Swift String Set
In the following example, we initialize a Set of Strings containing names of students, and we find the size of this Set using count property.
main.swift
var names: Set = ["John", "Rafel", "Sindhu"]
var size = names.count
print("Size of Set is: \(size)")
Output
Size of Set is: 3
How Duplicate Values Affect Swift Set Size
A Set keeps only one instance of each value. If the same value appears more than once in a Set literal, the duplicates do not increase its size.
let numbers: Set<Int> = [10, 20, 10, 30, 20]
print(numbers.count)
Output
3
Although five values appear in the literal, the resulting Set contains only 10, 20, and 30. Its count is therefore 3.
Find the Size of an Empty Swift Set
The count property returns 0 when the Set contains no elements.
let emptySet = Set<String>()
print("Number of elements: \(emptySet.count)")
Output
Number of elements: 0
Check Whether a Swift Set Is Empty
Use count when you need the exact number of elements. When you only need to determine whether the Set has any values, use the isEmpty property instead.
let usernames: Set<String> = []
if usernames.isEmpty {
print("The Set is empty.")
} else {
print("The Set contains \(usernames.count) values.")
}
Output
The Set is empty.
Update the Set and Read Its New Count
The value returned by count reflects the Set’s current contents. Inserting a new unique element increases the count, while removing an existing element decreases it.
var colors: Set<String> = ["Red", "Blue"]
print(colors.count)
colors.insert("Green")
print(colors.count)
colors.insert("Red")
print(colors.count)
colors.remove("Blue")
print(colors.count)
Output
2
3
3
2
Inserting Red for a second time does not change the count because that value is already present in the Set.
Swift Set count and capacity Are Different
The count and capacity properties describe different things:
countis the number of elements currently stored in the Set.capacityis the number of elements the Set can hold before it may need to allocate additional storage.
Use count to answer questions such as “How many unique values are in this Set?” Do not use capacity as the Set’s logical size.
Frequently Asked Questions About Swift Set Size
What is the length of a Set in Swift?
The length or size of a Swift Set is the number of unique elements it contains. Read this value using the Set’s count property.
What type of value does Set.count return?
Set.count returns an Int.
Do duplicate values increase a Swift Set’s count?
No. A Set stores unique values, so inserting or initializing it with a duplicate value does not increase its count.
Should I use count or isEmpty for an empty Set check?
Use isEmpty when you only need a Boolean empty-or-not-empty result. Use count when you need the exact number of elements.
Does the order of Set elements affect count?
No. Swift Sets are unordered collections, and their iteration order does not affect the value returned by count.
Swift Set Size Editorial QA Checklist
- Confirm every example uses
Set.countto obtain the number of stored elements. - Verify that duplicate-value examples report only the number of unique values.
- Keep
count,capacity, andisEmptyexplanations distinct. - Check that each displayed output matches the corresponding Swift program.
- Use an explicit Set type where an empty collection or type inference could be ambiguous.
Summary: Getting the Number of Elements in a Swift Set
Use the count property to get the current number of unique elements in a Swift Set. An empty Set has a count of 0, duplicate values do not increase the count, and changes made with operations such as insert and remove are reflected the next time the property is read.
In this Swift Tutorial, we have learned to get the size of a Set using count property with the help of example programs.
TutorialKart.com