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
+13.6k Golang : Compress and decompress file with compress/flate example
+4.5k Fix Google Analytics Redundant Hostnames problem
+45.8k Golang : Marshal and unmarshal json.RawMessage struct example
+13.5k Golang : Human readable time elapsed format such as 5 days ago
+9.2k Golang : Changing a RGBA image number of channels with OpenCV
+6.9k Golang : Check if one string(rune) is permutation of another string(rune)
+20.3k Golang : Saving private and public key to files
+7.8k Golang : Multiplexer with net/http and map
+18.1k Golang : Find IP address from string
+45.9k Golang : Encode image to base64 example
+5.1k Golang *File points to a file or directory ?