I am a beginner with go and i am experimenting with different data types, I want to know how to find the minimum value in a map as it could be useful later and im curious.
These are two functions of two different attempts to get this working, neither worked, some showed me the maximum value of a map
func printMinValue() {
// Check if the number of entries in the map is less than gridWidth
if len(heightMapping) < gridWidth {
fmt.Println(0)
return
}
// Find the minimum value in the map
minValue := math.MaxInt32
for _, value := range heightMapping {
if value < minValue {
minValue = value
}
}
// Print the minimum value if valid
if minValue == math.MaxInt32 {
fmt.Println(0)
} else {
fmt.Println(minValue)
}
}
func findMinValue(m map[int]int) (int, error) {
if len(m) == 0 {
return 0, fmt.Errorf("map is empty")
}
minValue := math.MinInt // Start with the maximum possible integer value
for _, value := range m {
if gridHeight-value > minValue {
minValue = value
}
}
New contributor
spider_under_your_bed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.