Function to extract the maximum and minimum values ​​in a slice with Go

Introduction

When writing code in Go, the process of calculating the maximum value or the minimum value in the slice I used it several times, so make a note so that I don't forget it.

Get the maximum value in a slice

func maxInt(a []int) int {
    sort.Sort(sort.IntSlice(a))
    return a[len(a)-1]
}

Get the minimum value in a slice

func minInt(a []int) int {
    sort.Sort(sort.IntSlice(a))
    return a[0]
}

Sample code

Copy and execute the following code with The Go Playground

package main
import "fmt"
import "sort"

func main() {
	test := []int{7, 8 ,1, 4, 3, 21}

	fmt.Println("max:", maxInt(test))
	fmt.Println("min:", minInt(test))
}


func maxInt(slice []int) int {
    sort.Sort(sort.IntSlice(slice))
    return slice[len(slice)-1]
}


func minInt(slice []int) int {
    sort.Sort(sort.IntSlice(slice))
    return slice[0]
}

reference

Recommended Posts

Function to extract the maximum and minimum values ​​in a slice with Go
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
I also tried to imitate the function monad and State monad with a generator in Python
Create a function to get the contents of the database in Go
[Python] A program that finds the minimum and maximum values without using methods
How to extract null values and non-null values with pandas
Extract the maximum value with pandas and change that value
How to get all the keys and values in the dictionary
To write a test in Go, first design the interface
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Recursively get the Excel list in a specific folder with python and write it to Excel.
The story I was addicted to when I specified nil as a function argument in Go
Extract the maximum value with pandas.
How to get all the possible values in a regular expression
[Introduction to Python] How to split a character string with the split function
Attempt to extend a function in the library (add copy function to pathlib)
How to divide and process a data frame using the groupby function
Replace the directory name and the file name in the directory together with a Linux command.
[Python] Explains how to use the range function with a concrete example
How to get the date and time difference in seconds with python
[Python] How to use the enumerate function (extract the index number and element)
Added a function to register desired shifts in the Django shift table
[Introduction to Python] How to write a character string with the format function
How to pass the path to the library built with pyenv and virtualenv in PyCharm
Feel free to write a test with nose (in the case of + gevent)
[Python] Created a class to play sin waves in the background with pyaudio
To output a value even in the middle of a cell with Jupyter Notebook
Extract the color of the object in the image with Mask R-CNN and K-Means clustering
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Zip-compress any file with the [shell] command to create a file and delete the original file.
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
[Spoiler note] Go to predict the bride in five equal parts with PyTorch
I want to get the file name, line number, and function name in Python 3.4
On Linux (Ubuntu), tune the Trackpad and set the function to a three-finger swipe
A tool to insert the country name and country code in the IP address part
How to get a list of files in the same directory with python
Log in to the remote server with SSH
[Python] Get the files in a folder with Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
[Automation] Extract the table in PDF with Python
Save the object to a file with pickle
A story packed with absolute values in numpy.ndarray
[Go] How to write or call a function
How to Mock a Public function in Pytest
Add a function to return the minimum value (min) to the stack made with Python, but push / pop / min is basic O (1) !!
Run the output code with tkinter, saying "A, pretending to be B" in python
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
Automate background removal for the latest portraits in a directory with Python and API
How to identify the element with the smallest number of characters in a Python list?
Extract images and tables from pdf with python to reduce the burden of reporting
How to unit test a function containing the current time using freezegun in python
[Note] How to write QR code and description in the same image with python
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
I just wanted to extract the data of the desired date and time with Django
[Mac] A super-easy way to execute system commands in Python and output the results
I wrote a CLI tool in Go language to view Qiita's tag feed with CLI
[Python] How to handle inf and NaN in numpy mean, standard deviation, maximum / minimum
I want to extract only pods with the specified label using Label Selector in Client-go
A solution to the problem that files containing [and] are not listed in glob.glob ()