Swift program to check a set collection is empty or not

Here, we are going to learn how to check a set collection is empty or not in Swift programming language?
Submitted by Nidhi, on June 28, 2021

Problem Solution:

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.

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.

// 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.

Swift Set Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.