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
+16.1k Golang : Defer function inside init()
+18k Golang : How to get own program name during runtime ?
+13.6k Golang : Search folders for file recursively with wildcard support
+27.8k Golang : How to create new XML file ?
+16.4k How to enable MariaDB/MySQL logs ?
+13.4k Golang : package is not in GOROOT during compilation
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+6.6k Golang : Generate human readable password
+5.1k Golang : Experimenting with the Rejang script
+11k Golang : List running EC2 instances and descriptions
+8.2k Golang : Extract or copy items from map based on value
+38.8k Golang : How do I convert int to uint8?