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 Nginx and PageSpeed build from source CentOS example
+9.2k Golang : How to tokenize source code with text/scanner package?
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+3.6k Javascript : Detect when console is activated and do something about it
+19.3k Golang : Saving private and public key to files
+10.9k Golang : Simple client-server HMAC authentication without SSL example
+17.9k Golang : Get RGBA values of each image pixel
+7.3k Golang : How to check variable or object type during runtime?
+5.9k Mac/Linux/Windows : Get CPU information from command line
+13.6k Golang : Execute function at intervals or after some delay
+11.3k Golang : Arithmetic operation with numerical slices or arrays example
+8.3k Golang : Detect number of active displays and the display's resolution