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
+20.1k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+39.4k Golang : How to read CSV file
+7.1k Android Studio : Hello World example
+21.5k Golang : Create and resolve(read) symbolic links
+7.3k Nginx : How to block user agent ?
+5.2k Golang : Calculate a pip value and distance to target profit example
+8.3k Golang : Reverse text lines or flip line order example
+18.9k Golang : Iterating Elements Over A List
+22.9k Golang : Round float to precision example
+20.1k Golang : Measure http.Get() execution time
+6.2k nginx : force all pages to be SSL
+10.9k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example