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
+7.6k Golang : Process json data with Jason package
+30.7k Golang : Remove characters from string example
+8.4k Prevent Write failed: Broken pipe problem during ssh session with screen command
+6.3k Golang : Grab news article text and use NLP to get each paragraph's sentences
+62.9k Golang : Convert HTTP Response body to string
+24.7k Golang : GORM read from database example
+26k Golang : Daemonizing a simple web server process example
+14.7k How to automatically restart your crashed Golang server
+22.6k Golang : How to read JPG(JPEG), GIF and PNG files ?
+9.5k Golang : Terminate-stay-resident or daemonize your program?
+13.4k Golang : Date and Time formatting
+11k Golang : Sieve of Eratosthenes algorithm