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
+13.4k Golang : How to filter a map's elements for faster lookup
+26.9k Golang : Connect to database (MySQL/MariaDB) server
+22.9k Golang : How to validate URL the right way
+35.3k Golang : Convert(cast) int64 to string
+6.1k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+11.8k Golang : http.Get example
+11.3k Golang : Validate email address
+15.5k Golang : How to implement two-factor authentication?
+14.9k Golang : How to check if input from os.Args is integer?
+11.5k Golang : "https://" not allowed in import path
+12.3k Golang : Read XML elements data with xml.CharData example