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
+11.2k Golang : Find age or leap age from date of birth example
+4.9k Golang : Experimental Jawi programming language
+4.6k Fix Google Analytics Redundant Hostnames problem
+14.2k Golang : Find network of an IP address
+25k Golang : Daemonizing a simple web server process example
+16.1k CodeIgniter/PHP : Create directory if does not exist example
+9.3k Golang : Validate IPv6 example
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+18.2k Golang : Find IP address from string
+8.9k Golang : Apply Histogram Equalization to color images
+5.9k Golang : Extract XML attribute data with attr field tag example