Golang Program to Find the Largest Element in an Array


In this tutorial, we will see to write a go language program to search the largest element in an array. an array is a variable that is used to store elements at contiguous memory locations. We can find the largest element of an array by either using a separate function or by using for loop and if conditions in the main() function.

Finding the Largest Element in an Array in Main Function()

The following code illustrates how we can search and find the largest element present in an array.

Algorithm

STEP 1 − Import the fmt package that allows us to print anything on the screen.

STEP 2 − Start the main function. This is the executable part of the program from where the execution starts.

STEP 3 − Initialize the variables of type int viz temp, largestNumber and j.

STEP 4 − These variables will be used later on in the program to carry the initial and largest number respectively.

STEP 5 − Make an array of integers and store values to it and print it on the screen using fmt.Println() function.

STEP 6 − Now use the for loop to iterate over each element of the array and use the if condition to check whether the current element of the array is greater than the previously stored number.

STEP 7 − If the number found is greater than the previous one store the number in largestNumber variable

STEP 8 − upgrade the temp count and store the current index in a int variable j.

STEP 9 − Repeat the process for the complete array

STEP 10 − Once the process is over print the result on the screen along with the position of the element using fmt.Println() function.

Example

Find the largest element of an array we are using for loop and conditionals in this program.

package main
import "fmt"
func main() {
   var largestNumber, temp, j int
   arr := [5]int{100, -20, 300, 40, -50}
   fmt.Println("The unsorted array entered is:", arr)
   for i, element := range arr {
         if element > temp {
         temp = element
         largestNumber = temp
         j = i
      }
   }
   fmt.Println("Largest number of Array is ", largestNumber, "and its position is: ", j)
}

Output

The unsorted array entered is: [100 -20 300 40 -50]
Largest number of Array is  300 and its position is:  2

Finding Largest Number in Array using two Different Functions

Now let us see how we can get the largest element of the array using a user defined function. This function accepts an array of integers as an argument and returns the final result which we can store and print on the screen using fmt.Println() function.

Algorithm:

STEP 1 − Import the fmt package that allows us to print anything on the screen.

STEP 2 − Make a function named findMaxElement(). This function returns the largest value of the array along with its position.

STEP 3 − Use for loop to iterate over the array to obtain the largest integer value.

STEP 4 − Start the main() function.

STEP 5 − Make an array of integers and store values to it. Print the array on the screen using fmt.Println() function.

STEP 6 − Call the findMaxElement() function by passing the array as an argument to the function and store the result obtained in separate variables.

STEP 7 − Print the greatest value on the screen along with the position of the element using fmt.Println() function.

STEP 8 − Repeat the process by getting some more arrays.

Example

In this example, we are going to use a user defined function outside main function.

package main
import "fmt"

// Creating a function called getLargestValue
func findMaxElement(arr []int) (int, int) {
   largestNumber := arr[0]
   var k int
   for i := 0; i < len(arr); i++ {
      if arr[i] > largestNumber {
         largestNumber = arr[i]
         k = i
      }
   }
   return largestNumber, k
}
func main() {
   arr := []int{1, 2, 3, 4, -5, -10}
   fmt.Println("The first array is:", arr)
   result, index := findMaxElement(arr)
   fmt.Println("The largest number of the array is: ", result, "and its position is:", index)
   fmt.Println()
   arr = []int{10, 8, 0, 20, 30}
   fmt.Println("The second array is:", arr)
   result, index = findMaxElement(arr)
   fmt.Println("The largest number of the array is: ", result, "and its position is:", index)
}

Output

The first array is: [1 2 3 4 -5 -10]
The largest number of the array is:  4 and its position is: 3

The second array is: [10 8 0 20 30]
The largest number of the array is:  30 and its position is: 4

Conclusion

In this tutorial, we have successful executed the program to find largest number in the array.In the first example, we are implementing the logic in the main() section of the program while in the second example, we are using an external user defined function to implement the result.

Updated on: 10-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements