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.2k Javascript : Generate random key with specific length
+23.3k Golang : Get ASCII code from a key press(cross-platform) example
+4.6k Adding Skype actions such as call and chat into web page examples
+16k Golang : Generate universally unique identifier(UUID) example
+6.8k Golang : Normalize email to prevent multiple signups example
+36.2k Golang : Save image to PNG, JPEG or GIF format.
+13.8k Golang : Get current time
+5.3k Golang : Intercept, inject and replay HTTP traffics from web server
+19.5k Golang : Get current URL example
+6.6k Golang : When to use make or new?
+41.4k Golang : Convert string to array/slice
+37.1k Golang : Converting a negative number to positive number