- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find the largest element in a slice
In this tutorial, we will inculcate how to find the largest element in a slice using some examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means 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’s go through the example to understand things.
Method 1: Using a user-defined function and append() function
In this method, we will see how to find the largest element in a slice using a user-made function with slice as parameter in it. The output will be printed on the console using fmt.Println() function. Let’s see through the code how is all being done.
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 add values in it using append function.
Step 3 − Print the slice on the console using print statement in Golang.
Step 4 − From the main call the user-defined function largest_ele with slice as a parameter.
Step 5 − In the function largest_ele set the first element in slice equal to max which means the first element is initialized as maximum element.
Step 6 − Run a loop till the length of slice and check if the element of slice is greater than max element.
Step 7 − If its true set max equals to the slice element and continue the same until one maximum element is found.
Step 8 − Return the maximum element back to the function, this maximum element is printed on the console using fmt.Println() function.
Example
Golang program to find the largest element in a slice using Using a user-defined function and append() function
package main import "fmt" func main() { // Create a slice of integers var slice []int slice = append(slice, 10) // create slice using append function slice = append(slice, 20) slice = append(slice, 30) slice = append(slice, 40) slice = append(slice, 50) fmt.Println("The original slice is:", slice) val := largest_ele(slice) fmt.Println("The largest element in the slice is:") fmt.Println(val) // print the largest element } func largest_ele(slice []int) int { var max int max = slice[0] // assign slice first element to max for _, ele := range slice { if ele > max { //check whether element is greater than max max = ele } } return max }
Output
The original slice is: [10 20 30 40 50] The largest element in the slice is: 50
Method 2: Using math function
In this tutorial, we will find the largest element in slice using max function which will compare the elements from the loop that which is the maximum. Let’s have a look and get a clear understanding of this example.
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 add values in it using append function.
Step 3 − Print the slice on the console using print statement in Golang.
Step 4 − From the main call the user-defined function largest_ele with slice as a parameter.
Step 5 − In the function largest_ele set the first element in slice equal to max which means the first element is initialized as maximum element.
Step 6 − Run a loop till the length of slice and check if the element of slice is greater than max element using max function on the max_num variable.
Step 7 − Continue the same until one maximum element is found.
Step 8 − Return the maximum element max_num back to the function, this maximum element is printed on the console using fmt.Println() function.
Example
Golang program to find largest element in a slice using math function
package main import ( "fmt" "math" ) func main() { var slice []int slice = append(slice, 10) // create slice using append function slice = append(slice, 20) slice = append(slice, 30) slice = append(slice, 40) slice = append(slice, 50) fmt.Println("The original slice is:", slice) maximum := largest_ele(slice) fmt.Println("The largest element in slice is:") fmt.Println(maximum) // print maximum element } func largest_ele(slice []int) int { var max_num int max_num = slice[0] //assign slice’s first element to max_num for _, element := range slice { max_num = int(math.Max(float64(max_num), float64(element))) } return max_num // return the maximum element to the function }
Output
The original slice is: [10 20 30 40 50] The largest element in slice is: 50
Conclusion
We executed the program of finding the largest element from a slice using two examples. In the first example we used a custom function to find the largest element and in the second example we used max function to find the largest element from the slice. Both the examples give similar output. Hence the program executed successfully.