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
+11.3k Golang : Fix go.exe is not compatible with the version of Windows you're running
+6.8k Golang : When to use make or new?
+9.5k Golang : How to get garbage collection data?
+17.4k Golang : Find file size(disk usage) with filepath.Walk
+10.8k Golang : How to delete element(data) from map ?
+24.7k Golang : Time slice or date sort and reverse sort example
+14k Golang : Convert spaces to tabs and back to spaces example
+5.2k Swift : Convert (cast) Float to Int or Int32 value
+8.4k Golang : Qt splash screen with delay example
+10.1k Golang : Check if user agent is a robot or crawler example
+6.4k Golang : How to get capacity of a slice or array?
+10.8k Golang : Allow Cross-Origin Resource Sharing request