# Golang program to find minimum element in array using linear search

In this Golang article, we will learn how to find the minimum element in an array using linear search by using recursion and iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript.

Linear search is a simple search algorithm that checks every element in a list or array one by one, starting from the beginning, until a target value is found or the entire list has been searched. It is also known as sequential search.

## Syntax

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


The minFind() function is used to find the minimum element in array using linear search. It takes an integer array as an argument to it and returns the minimum element by using linear search.

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


The minFind() function is used to find the minimum element in array using linear search. It takes an integer array and length of the array as arguments to it and returns the minimum element by using linear search.

## Method 1

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

### Algorithm

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

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

• Step 3 − Firstly, it initializes the min variable to the first element in the array, and then iterate over the array using a for loop.

• Step 4 − If it finds any number that is smaller than the current minimum, it updates the value of min to that number and returns the value of min.

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

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

• Step 7 − Further, the minimum element is printed on the screen by using the fmt.Printf() function.

### Example

In the following example, we will create a go language program to find minimum element in array using linear search by using iterative method

package main

import "fmt"

func minFind(arr []int) int {
min := arr[0]
for _, num1 := range arr {
if num1 < min {
min = num1
}
}
return min
}

func main() {
arr := []int{20, 10, 45, 8, 12}
min := minFind(arr)
fmt.Printf("The minimum element in the array is: %d", min)
}


### Output

The minimum element in the array is: 8


## Method 2

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

### Algorithm

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

• Step 2 − Now, create a minFind() function that recursively finds minimum element in an array using linear search.This function takes an array of integers and the length of the array as input.

• Step 3 − Then, base case is defined when the length of the array is 1 and simply the first element of the array is returned.

• Step 4 − Otherwise, make recursive call to minFind(0 function with the length of the array reduced to 1 and store the result in the min variable.

• Step 5 − Then, check if the last element of the array is smaller than min and if it is so, return that value.

• Step 6 − Finally, the value of min is returned back as the minimum element in the given array.

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

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

• Step 9 − Further, the minimum element is printed on the screen by using the fmt.Printf() function.

### Example

Following is the go language program to find minimum element in array using linear search by using recursive approach

package main

import "fmt"

func minFind(arr []int, n int) int {
if n == 1 {
return arr[0]
}
min := minFind(arr, n-1)
if arr[n-1] < min {
return arr[n-1]
}
return min
}

func main() {
arr := []int{10, 23, 37, 19, 42}
min := minFind(arr, len(arr))
fmt.Printf("The minimum element in the array is: %d", min)
}


### Output

The minimum element in the array is: 10


## Conclusion

We have successfully compiled and executed a go language program to find minimum 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. The resultant minimum element from the array passed is printed to the console as output using linear search.

Updated on: 10-May-2023

154 Views