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
+6.8k Android Studio : Rating bar example
+8.4k Golang : Qt get screen resolution and display on center example
+14.2k Golang : Delete certain files in a directory
+10.4k Golang : Byte format example
+7.9k Golang : Take screen shot of browser with JQuery example
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+6.8k Golang : get the current working directory of a running program
+6.3k Golang : File system scanning
+7.2k How to show different content from website server when AdBlock is detected?
+5.7k Get Facebook friends working in same company