Golang program to find maximum element in array using linear search


In this Golang article, we will learn how to find maximum element in array using linear search by using recursion and iterative method.

Syntax

func findMax(arr []int) int {…}

The findMax() function is used to find maximum element in array using linear search iteratively. It takes an integer array as argument to it.

func findMax(arr []int, n int) int {…}

The findMax() function is used to find maximum element in array using linear search recursively. It takes an integer array and the size of the array as arguments to it.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, create a findMax() function that finds the maximum element in an array using linear search.

  • Step 3 − It takes an array as input and returns the maximum element present in the array.

  • Step 4 − The function returns -1, if the array is empty or if array length is equal to 0.

  • Step 5 − If the array is not empty, it initializes a variable max with the first element of the array.

  • Step 6 − Then, it iterates the array and updates the max value if any element has greater value than the current value of the max.

  • Step 7 − Start the main() function. Inside the main() function, create an array with some elements.

  • Step 8 − Now, call the findMax() function and pass the array as argument to the function.

  • Step 9 − Further, the resultant maximum element from the array and the array defined are printed on the screen by using the fmt.Println() function.

Example1: Go Language Program To Find Maximum Element In Array Using Linear Search By Using Iterative Method

In this example, we will define a findMax() function using iterative method that is used to find maximum element in an array using linear search.

package main

import "fmt"

func findMax(arr []int) int {
   if len(arr) == 0 {
      return -1
   }

   max := arr[0]
   for i := 1; i < len(arr); i++ {
      if arr[i] > max {
         max = arr[i]
      }
   }
   return max
}

func main() {
   arr := []int{13, 45, 67, 22, 97, 43, 74}

   max := findMax(arr)

   fmt.Println("Array defined is:", arr)
   fmt.Println("Maximum element in the array is:", max)
}

Output

Array defined is: [13 45 67 22 97 43 74]
Maximum element in the array is: 97

Example 2

In this example, we will define a findMax() function using recursive method that is used to find maximum element in an array using linear search.

package main

import "fmt"

func findMax(arr []int, n int) int {
   if n == 1 {
      return arr[0]
   }

   max := findMax(arr, n-1)

   if arr[n-1] > max {
      return arr[n-1]
   }
   return max
}

func main() {
   arr := []int{13, 25, 67, 42, 80, 56, 44}
   
   max := findMax(arr, len(arr))

   fmt.Println("Array defined is:", arr)
   fmt.Println("Maximum element in the array is:", max)
}

Output

Array defined is: [13 25 67 42 80 56 44]
Maximum element in the array is: 80

Conclusion

We have successfully compiled and executed a go language program to find maximum element in an array using linear search by using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.

Updated on: 10-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements