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
+3.9k JQuery : Calling a function inside Jquery(document) block
+8.1k Golang : Create and shuffle deck of cards example
+10.1k Golang : Find age or leap age from date of birth example
+6.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+12.3k Golang : Strings comparison
+5.4k Grep : How to grep for strings inside binary data
+18.3k Golang : How to Set or Add Header http.ResponseWriter?
+10k Golang : How to use if, eq and print properly in html template
+29.6k error: trying to remove "yum", which is protected
+7.5k Golang : How to join strings?
+4.4k Golang : Return multiple values from function
+21.2k Golang : Set and Get HTTP request headers example