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
+14.1k nginx: [emerg] unknown directive "ssl"
+8.1k Golang : Take screen shot of browser with JQuery example
+16.5k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+8.7k Golang : How to generate Code 39 barcode?
+15.3k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+10.1k Golang : Natural string sorting example
+7.3k Golang : Qt splash screen with delay example
+15.7k Golang : read gzipped http response
+8.1k Golang : Populate or initialize struct with values example
+19.1k Golang : Count number of digits from given integer value
+11k Golang : Clean formatting/indenting or pretty print JSON result
+13.7k Golang : Get URI segments by number and assign as variable example