Here, we will create a set of integer elements and also created an empty set using the Set collection. Then we will check sets are empty or not using the isEmpty property.
The source code to check a set collection is empty or not is given below. The given program is compiled and executed successfully.
// Swift program to check a set collection
// is empty or not
import Swift
var IntSet:Set = [1,2,3,4]
var EmptySet = Set<Int>()
if(IntSet.isEmpty)
{
print("IntSet is empty")
}
else
{
print("IntSet is not empty")
}
if(EmptySet.isEmpty)
{
print("EmptySet is empty")
}
else
{
print("EmptySet is not empty")
}
Output:
IntSet is not empty
EmptySet is empty
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
import Swift
Here, we created two sets IntSet, EmptySet. Then we checked created sets are empty or not using the isEmpty property and printed the appropriate messages on the console screen.
Program/Source Code:
The source code to check a set collection is empty or not is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
Here, we created two sets IntSet, EmptySet. Then we checked created sets are empty or not using the isEmpty property and printed the appropriate messages on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer