Go 1.21’s min() Function

Solving Leetcode’s simplest problems with Go 1.21’s new builtin functions.

Cheikh seck
Stackademic

--

https://unsplash.com/photos/VHNLKLNlCA8Kier in Sight Archives

One of the first Leetcode problems I encountered involved finding the smallest value within a set of numbers. During those times, I was solving these problems with Javascript and taking advantage of it’s built-in min and max functions. I’m a huge fan of the Go language, however, solving such Leetcode challenges with Go was impractical — until recently. In this post, I’m going to demonstrate two approaches to finding the smallest value within a set of numbers:

  • Implement a range loop and use conditional statements to determine the smallest value.
  • Use Go’s new builtin min function with a range loop.

The Range Approach

The first approach I’m going to look at is to look for the smallest number with a range loop (a for loop) and a conditional statement.

Listing 1

01 package main
02
03 import "log"
04
05 func main() {
06
07 set := []int{-3, 0, 10, 23, 18, -5}
08 smallest := 0
09
10 for _, v := range set {
11
12 if v > smallest {
13 continue
14 }
15
16 smallest = v
17 }
18
19 log.Println("Smallest value is", smallest)
20
21 }
22

Listing 1 depicts a program that solves for the smallest value within a set of numbers. In line 7, I’m declaring an array with various integers. In line 8, I’m defining a variable that will store the smallest value within the set. In line 10, I begin looping over the array and on line 12, I’m checking to see if v is greater than the smallest value; if so, I’ll continue, as seen in line 13; by setting this condition, I’m ensuring the code remains readable because the actual problem solving logic remains at the highest level of the loop.

In line 16, I’m setting the smallest value to v as it passed all the checks I had before hand.

This Code Is Terrible

The code in listing 1 will break if all the values in the provided set are greater than 0; this happens because none of the values are technically less than 0 which is the default value of variable smallest that is compared with a value from the set. One way to solve this is to change the type variable smallest to have a pointer but… The scent of duct tape’d code rabbit hole will only grow stronger with each “small” fix; and to avoid going down this path, I’m going to use Go’s new builtin function.

Finding the smallest value with function `min`

The next approach I’m going to look at is with builtin function min.

Listing 2

01 package main
02
03 import "log"
04
05 func main() {
06
07 set := []int{-3, 0, 10, 23, 18, -5}
08 smallest := set[0]
09
10 for _, v := range set {
11
12 smallest = min(smallest, v)
13 }
14
15 log.Println("Smallest Value is", smallest)
16
17 }
18

Listing 2 depicts an implementation to find the smallest value within a set with function min . In line 08, I’m setting the first value of the set as the default smallest value; this will fix the bug presented in the previous implementation. On line 12, I’m borrowing some Javascript and setting variable smallest to result of function min — which should be the smallest value between the two numbers passed.

The general assumption with this approach is that there is atleast one value within the set; this is important to keep in mind because passing an empty array will result in a panic.

Conclusion

I initially tried to pass an array as the second parameter of builtin function min; the compiler quickly shut me down, hence the implementation with the range loop. I like this function because it signifies having to write one less conditional statement to determine the minimum value within a set of numbers.

You can read more about the min function here: (while your there, checkout the max function too)

Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.

--

--