To find the smallest element in an array in Swift, you can use the min()
method provided by the standard library. Here's an example:
main.swift102 chars5 lines
In this example, we define an array of integers called numbers
. We then call the min()
method on the array, which returns the smallest element in the array as an optional value. We then print the value of smallest
to the console.
Note that the min()
method returns an optional value in case the array is empty, so you may want to use optional binding to safely unwrap the result:
main.swift127 chars6 lines
gistlibby LogSnag