Swift – Create an Empty Set

To create an empty set in Swift, use Set<T>() where T is the type of elements we would like to store in this Set.

The following statements are some of the examples to create empty set.

var strs = Set<String>()
var nums = Set<Int>()

Examples

In the following program, we will initialize an empty Set that stores Strings.

main.swift

var names = Set<String>()
print(names)

Output

Swift - Create an Empty Set of Strings

Now, let us create an empty Set that stores Integers.

main.swift

var names = Set<Int>()
print(names)

Output

Swift - Create an Empty Set of Integers
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned how to create an Empty Set, in Swift programming.