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
+4.9k Golang : Calculate a pip value and distance to target profit example
+11.9k Golang : Convert(cast) bigint to string
+9.3k Golang : How to get garbage collection data?
+4.9k JQuery : Calling a function inside Jquery(document) block
+11.7k Golang : How to detect a server/machine network interface capabilities?
+8.7k Golang : Gorilla web tool kit schema example
+17.6k Convert JSON to CSV in Golang
+8.7k Golang : On lambda, anonymous, inline functions and function literals
+22.7k Golang : Strings to lowercase and uppercase example
+9.9k Golang : Function wrapper that takes arguments and return result example
+35.9k Golang : Get file last modified date and time
+6.2k Golang : How to get capacity of a slice or array?