Q:

Swift program to check a set collection is empty or not

belongs to collection: Swift Set Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

total answers (1)

Swift program to sort the elements of Set collecti... >>
<< Swift program to compare two sets using the equal ...