To create a set of size N in Swift, you can use the Set
type with an array of initial elements.
In this example,
stringElements
with initial string elements.stringSet
using the Set
type and initialize it with the array stringElements
.count
property.let stringElements = ["apple", "banana", "cherry"]
let stringSet = Set(stringElements)
print("Size of stringSet: \(stringSet.count)")
Size of stringSet: N
In this example,
intElements
with initial integer elements.intSet
using the Set
type and initialize it with the array intElements
.count
property.let intElements = [1, 2, 3]
let intSet = Set(intElements)
print("Size of intSet: \(intSet.count)")
Size of intSet: N
In this tutorial, we learned How to create a Set of size N in Swift language with well detailed examples.