Golang : Find smallest number in array
Alright, today I'll just write this simple tutorial on how to find the smallest value in an array. Basically, the algorithm is just iterate through the values inside the given array and replace the final value( which is the variable min) with the smallest value it can find.
Here's the code :
package main
import "fmt"
func main() {
arr := []uint{
28, 33, 16,
7, 5, 88,
}
min := arr[0] // assume first value is the smallest
for _, value := range arr {
if value < min {
min = value // found another smaller value, replace previous value in min
}
}
fmt.Println("The smallest value is : ", min)
}
Output :
The smallest value is : 5
See also : Golang : Find biggest/largest number in array
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.5k Elasticsearch : Shutdown a local node
+6.2k Apt-get to install and uninstall Golang
+7.8k Golang : Ways to recover memory during run time.
+12.1k Golang : calculate elapsed run time
+10.5k Golang : Allow Cross-Origin Resource Sharing request
+8.9k Golang : Capture text return from exec function example
+11.1k CodeIgniter : How to check if a session exist in PHP?
+31.2k Golang : bufio.NewReader.ReadLine to read file line by line
+10k Golang : Print how to use flag for your application example
+10.5k Golang : Bubble sort example
+18.4k Golang : Send email with attachment