- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Write a program in Go language to find the element with the maximum value in an array
Examples
- A1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Maximum number is 10
- A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Maximum number is 110
Approach to solve this problem
Step 1: Consider the number at the 0th index as the maximum number, max_num = A[0]
Step 2: Compare max_num with every number in the given array, while iterating.
Step 3: If a number is greater than max_num, then assign that number to max_num;
Step 4: At the end of iteration, return max_num;
Program
package main import "fmt" func findMaxElement(arr []int) int { max_num := arr[0] for i:=0; i<len(arr); i++{ if arr[i] > max_num { max_num = arr[i] } } return max_num } func main(){ arr := []int{2, 3, 5, 7, 11, 13} fmt.Println(findMaxElement(arr)) fmt.Println(findMaxElement([]int{2, 4, 6, 7, 8, 10, 3, 6, 0, 1})) fmt.Println(findMaxElement([]int{12, 14, 16, 17, 18, 110, 13, 16, 10, 11})) }
Output
13 10 110
- Related Articles
- Write a Golang program to find the element with the minimum value in an array
- PHP program to find the maximum element in an array
- Program to find maximum XOR with an element from array in Python
- Write a Java program to find the first array element whose value is repeated an integer array?
- Write a Golang program to find the frequency of an element in an array
- Program to find the minimum (or maximum) element of an array in C++
- Write a Golang program to find the frequency of each element in an array
- Write a program to find the index of particular element in an array in javascript?
- C# program to find maximum and minimum element in an array
- C++ Program to Find Maximum Element in an Array using Binary Search
- Write a Golang program to search an element in an array
- Write a program in C++ to find the top K frequent element in an array of integers
- How to find the maximum value of an array in JavaScript?
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Print pair with maximum AND value in an array in C Program.

Advertisements