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.7k Get Facebook friends working in same company
+21.9k Golang : Securing password with salt
+14k Golang : Chunk split or divide a string into smaller chunk example
+6k Golang : Extract XML attribute data with attr field tag example
+19k Golang : Execute shell command
+12k Golang : md5 hash of a string
+86.6k Golang : How to convert character to ASCII and back
+10.7k Golang : Sieve of Eratosthenes algorithm
+25.5k Golang : missing Mercurial command
+17.9k Golang : Convert IPv4 address to decimal number(base 10) or integer
+23.8k Golang : Find biggest/largest number in array
+9.7k Golang : Get current, epoch time and display by year, month and day