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
+7.9k Golang : How to check variable or object type during runtime?
+11.7k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+8k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+8.3k Golang : Sort lines of text example
+7.2k Golang : Example of how to detect which type of script a word belongs to
+8.4k Golang : HTTP Routing with Goji example
+5.3k List of Golang XML tutorials
+6.8k Golang : File system scanning
+5.1k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+4.8k Golang : Calculate half life decay example
+9.5k Golang : Get escape characters \u form from unicode characters
+12.7k Generate salted password with OpenSSL example