
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Maximum and Minimum from a Slice in Go
In this article, we will see how to fetch maximum and minimum elements from a slice. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover; they are passed by reference instead by value. Let us understand this basic concept using different set of examples and algorithms based upon them.
Method 1: Using the Helper Function
In this method, we will learn how to get the maximum and minimum elements from the slice using an external function. The min and max value obtained will be printed on the screen using fmt.Println() function which is used for printing the statements in Golang.
Syntax
func append(slice, element_1, element_2?, element_N) []T
The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function create a slice and some values inside it using append function.
Step 3 ? Call the function minandmax with the slice as parameter inside the function.
Step 4 ? In the function minandmax set the first element of slice equals to min and also equals to max.
Step 5 ? Run a loop till the length of the slice and check in the first case that is the number in slice is less than min value set the min value equal to the number.
Step 6 ? In the next condition check if the number is greater than max value set the max value equal to the number. Repeat the process until the loop is terminated.
Step 7 ? We obtain the min and max element which is returned to the function and printed using fmt.Println() function where ln refers to new line.
Example
Golang program to get maximum and minimum elements from slice using helper function
package main import ( "fmt" ) func main() { var values []int //creating slice values = append(values,10) //fill the elements in slice using append function values = append(values, 8) values = append(values, 20) values = append(values, 4) values = append(values, 56) fmt.Println("The slice containing the values is:", values) min, max := minandmax(values) fmt.Println("Minimum element in the slice is:", min) //print min element fmt.Println("Maximum element in the slice is:", max) //print max element } func minandmax(values []int) (int, int) { min := values[0] //assign the first element equal to min max := values[0] //assign the first element equal to max for _, number := range values { if number < min { min = number } if number > max { max = number } } return min, max }
Output
The slice containing the values is: [10 8 20 4 56] Minimum element in the slice is: 4 Maximum element in the slice is: 56
Method 2: Using the built-in Function Sort
In this method, we will see how to find the maximum and minimum element from the slice using built-in function sort. Let' see via algorithm and code how to execute this example.
Syntax
sort.Ints(slice)
The sort function in Golang is used to sort the elements in the slice. It is used to sort the strings, integer and float values. It sorts the values in ascending order with the slice as a parameter inside it.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 ? Along with these packages import sort package in the program to find min and max element.
Step 3 ? Create a function main and in that function create a slice with some values inside it.
Step 4 ? Use sort function to sort the slice and after use of this function we will get minimum and maximum element.
Step 5 ? Print the minimum and maximum element using fmt.Println() function where ln refers to new line.
Example
Golang program to get maximum and minimum elements from slice using the built-in function sort
package main import ( "fmt" "sort" ) func main() { values := []int{10, 8, 43, 52, 98} // create slice fmt.Println("The values in slice are:", values) sort.Ints(values) // sort the values finding min and max element fmt.Println("Minimum value in the slice is:", values[0]) //print min element fmt.Println("Maximum value in the slice is:", values[len(values)-1]) //print max element }
Output
The values in slice are: [10 8 43 52 98] Minimum value in the slice is: 8 Maximum value in the slice is: 98
Conclusion
We executed the program of finding the minimum and maximum element from the slice using two methods. In the first method, we used an external function to find the elements from the slice and in the second method; we used the sort function to print the maximum and minimum element