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
Write a Golang program to find the element with the minimum value in an array
Examples
- A1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Minimum number is 0;
- A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Minimum number is 10;
Approach to solve this problem
Step 1: Consider the number at the 0th index as the minimum number, min_num = A[0].
Step 2: Compare min_num with every number in the given array, while iterating.
Step 3: If a number is smaller than min_num, then assign that number to min_num.
Step 4: At the end of iteration, return min_num;
Program
package main
import "fmt"
func findMinElement(arr []int) int {
min_num := arr[0]
for i:=0; i<len(arr); i++{
if arr[i] < min_num {
min_num = arr[i]
}
}
return min_num
}
func main(){
arr := []int{2, 3, 5, 7, 11, 13}
fmt.Println(findMinElement(arr))
fmt.Println(findMinElement([]int{2, 4, 6, 7, 8, 10, 3, 6, 0, 1}))
fmt.Println(findMinElement([]int{12, 14, 16, 17, 18, 110, 13, 16, 10, 11}))
}
Output
2 0 10
Advertisements