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
+19.1k Golang : Get RGBA values of each image pixel
+8.8k Golang : Populate or initialize struct with values example
+5.8k AWS S3 : Prevent Hotlinking policy
+8.8k Golang : GMail API create and send draft with simple upload attachment example
+13.4k Golang : reCAPTCHA example
+6.5k Golang : Check if password length meet the requirement
+12.3k Golang : Search and extract certain XML data example
+5.5k Golang : Shortening import identifier
+5.9k Golang : Convert Chinese UTF8 characters to Pin Yin
+8.6k Golang : Combine slices but preserve order example
+21k Golang : Get password from console input without echo or masked
+46.2k Golang : Encode image to base64 example