Swift – Minimum Element of a Set

To find minimum element or lowest values of a Set in Swift, call min() method on this Set. The method returns the element that is minimum in value.

Examples

In the following example, we take a Set nums and find the minimum element in this Set.

main.swift

</>
Copy
let nums: Set = [2, 4, 6, 18, 10]
if let m = nums.min() {
    print("Minimum Element : \(m)")
}

Output

Minimum Element : 2
Program ended with exit code: 0

Now, let us take a Set of Strings fruits, and find the minimum value.

main.swift

</>
Copy
let fruits: Set = ["banana", "mango", "apple"]
if let m = fruits.min() {
    print("Minimum Element : \(m)")
}

Output

Minimum Element : apple
Program ended with exit code: 0

Conclusion

In this Swift Tutorial, we learned how to find the minimum element in a Set using Set.min() method.