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
+9.6k Golang : Sort and reverse sort a slice of integers
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+9.5k Golang : ffmpeg with os/exec.Command() returns non-zero status
+7.6k Golang : Regular Expression find string example
+5.7k Golang : Use NLP to get sentences for each paragraph example
+30.1k Golang : How to redirect to new page with net/http?
+11.3k CodeIgniter : Import Linkedin data
+10.8k Golang : Create S3 bucket with official aws-sdk-go package
+7.2k Golang : How to handle file size larger than available memory panic issue
+21.4k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+23.6k Golang : Use regular expression to validate domain name
+22.6k Golang : Calculate time different