Largest element in an Array Last Updated : 27 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given an array arr. The task is to find the largest element in the given array. Examples: Input: arr[] = [10, 20, 4]Output: 20Explanation: Among 10, 20 and 4, 20 is the largest. Input: arr[] = [20, 10, 20, 4, 100]Output: 100Table of ContentIterative Approach - O(n) Time and O(1) SpaceRecursive Approach - O(n) Time and O(n) SpaceUsing Library Methods - O(n) Time and O(1) SpaceIterative Approach - O(n) Time and O(1) SpaceThe approach to solve this problem is to traverse the whole array and find the maximum among them. C++ #include <iostream> #include <vector> using namespace std; int largest(vector<int>& arr) { int max = arr[0]; //Traverse from second and compare // every element with current max for (int i = 1; i < arr.size(); i++) if (arr[i] > max) max = arr[i]; return max; } int main() { vector<int> arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; } C #include <stdio.h> int largest(int arr[], int n) { int i; int max = arr[0]; // Traverse array elements from second and // compare every element with current max for (i = 1; i < n; i++) if (arr[i] > max) max = arr[i]; return max; } int main() { int arr[] = { 10, 324, 45, 90, 9808 }; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", largest(arr, n)); return 0; } Java import java.io.*; class GfG { static int largest(int[] arr) { int max = arr[0]; // Traverse array elements from second and // compare every element with current max for (int i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; } public static void main(String[] args) { int arr[] = { 10, 324, 45, 90, 9808 }; System.out.println(largest(arr)); } } Python def largest(arr, n): mx = arr[0] # Traverse array elements from second # and compare every element with # current max for i in range(1, n): if arr[i] > mx: mx = arr[i] return mx if __name__ == '__main__': arr = [10, 324, 45, 90, 9808] n = len(arr) ans = largest(arr, n) print(ans) C# using System; using System.Collections.Generic; class GfG { static int Largest(List<int> arr) { int max = arr[0]; // Traverse from second and compare // every element with current max for (int i = 1; i < arr.Count; i++) if (arr[i] > max) max = arr[i]; return max; } static void Main() { List<int> arr = new List<int> { 10, 324, 45, 90, 9808 }; Console.WriteLine(Largest(arr)); } } JavaScript function largest(arr) { let max = arr[0]; // Traverse from second and compare // every element with current max for (let i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; } // Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr)); Output9808Recursive Approach - O(n) Time and O(n) SpaceThe idea is similar to the iterative approach. Here the traversal of the array is done recursively instead of an iterative loop. C++ #include <iostream> #include <vector> using namespace std; int findMax(vector<int>& arr, int i) { // Last index returns the element if (i == arr.size() - 1) { return arr[i]; } // Find the maximum from the rest of the vector int recMax = findMax(arr, i + 1); // Compare with i-th element and return return max(recMax, arr[i]); } int largest(vector<int>& arr) { return findMax(arr,0); } int main() { vector<int> arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; } C #include <stdio.h> int findMax(int arr[], int size, int i) { // Last index returns the element if (i == size - 1) { return arr[i]; } // Find the maximum from the rest of the array int recMax = findMax(arr, size, i + 1); // Compare with i-th element and return return (recMax > arr[i]) ? recMax : arr[i]; } int largest(int arr[], int size) { return findMax(arr, size, 0); } int main() { int arr[] = {10, 324, 45, 90, 9808}; int size = sizeof(arr) / sizeof(arr[0]); printf("%d", largest(arr, size)); return 0; } Java import java.util.Arrays; class GfG { static int findMax(int[] arr, int i) { // Last index returns the element if (i == arr.length - 1) { return arr[i]; } // Find the maximum from the rest of the array int recMax = findMax(arr, i + 1); // Compare with i-th element and return return Math.max(recMax, arr[i]); } static int largest(int[] arr) { return findMax(arr, 0); } public static void main(String[] args) { int[] arr = {10, 324, 45, 90, 9808}; System.out.println(largest(arr)); } } Python def findMax(arr, i): # Last index returns the element if i == len(arr) - 1: return arr[i] # Find the maximum from the rest of the list recMax = findMax(arr, i + 1) # Compare with i-th element and return return max(recMax, arr[i]) def largest(arr): return findMax(arr, 0) if __name__ == '__main__': arr = [10, 324, 45, 90, 9808] print(largest(arr)) C# using System; class GfG { static int FindMax(int[] arr, int i) { // Last index returns the element if (i == arr.Length - 1) { return arr[i]; } // Find the maximum from the rest of the array int recMax = FindMax(arr, i + 1); // Compare with i-th element and return return Math.Max(recMax, arr[i]); } static int Largest(int[] arr) { return FindMax(arr, 0); } static void Main() { int[] arr = {10, 324, 45, 90, 9808}; Console.WriteLine(Largest(arr)); } } JavaScript function findMax(arr, i) { // Last index returns the element if (i === arr.length - 1) { return arr[i]; } // Find the maximum from the rest of the array let recMax = findMax(arr, i + 1); // Compare with i-th element and return return Math.max(recMax, arr[i]); } function largest(arr) { return findMax(arr, 0); } // Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr)); Output9808Using Library Methods - O(n) Time and O(1) SpaceMost of the languages have a relevant max() type in-built function to find the maximum element, such as std::max_element in C++. We can use this function to directly find the maximum element. C++ #include <iostream> #include <vector> #include <algorithm> using namespace std; int largest(vector<int>& arr) { return *max_element(arr.begin(), arr.end()); } int main() { vector<int> arr = {10, 324, 45, 90, 9808}; cout << largest(arr); return 0; } C #include <stdio.h> #include <stdlib.h> int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int largest(int arr[], int n) { qsort(arr, n, sizeof(int), compare); return arr[n - 1]; } int main() { int arr[] = {10, 324, 45, 90, 9808}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", largest(arr, n)); return 0; } Java import java.io.*; import java.util.*; class GfG { static int largest(int[] arr) { Arrays.sort(arr); return arr[arr.length - 1]; } static public void main(String[] args) { int[] arr = { 10, 324, 45, 90, 9808 }; System.out.println(largest(arr)); } } Python # Python program to find maximum in arr[] of size n def largest(arr): return max(arr) if __name__ == '__main__': arr = [10, 324, 45, 90, 9808] print(largest(arr)) C# using System; using System.Linq; using System.Collections.Generic; class GfG { static int largest(List<int> arr) { return arr.Max(); } static public void Main() { List<int> arr = new List<int> { 10, 324, 45, 90, 9808 }; Console.WriteLine(largest(arr)); } } JavaScript // Function to find the largest number in an array function largest(arr) { return Math.max(...arr); } // Driver Code const arr = [10, 324, 45, 90, 9808]; console.log(largest(arr)); Output9808 Comment More infoAdvertise with us Next Article Largest element in an Array kartik Follow Improve Article Tags : Searching Matrix DSA Arrays Basic Coding Problems Order-Statistics +2 More Practice Tags : ArraysMatrixSearching Similar Reads Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Find k largest elements in an array Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai 15+ min read Find Second largest element in an array | Set 2 Given an array arr[] consisting of N integers, the task is to find the second largest element in the given array using N+log2(N) - 2 comparisons. Examples: Input : arr[] = {22, 33, 14, 55, 100, 12}Output : 55 Input : arr[] = {35, 23, 12, 35, 19, 100}Output : 35 Sorting and Two-Traversal Approach: Re 9 min read Most frequent element in an array Given an array, the task is to find the most frequent element in it. If there are multiple elements that appear a maximum number of times, return the maximum element.Examples: Input : arr[] = [1, 3, 2, 1, 4, 1]Output : 1Explanation: 1 appears three times in array which is maximum frequency.Input : a 10 min read Largest element after K operations on Array Given array A[] (1 <= A[i] <= 108) of size N (2 <= N <= 103) and integer K (1 <= K <= 108), the task is to find the largest element of the array after performing the given operation at most K times, in one operation choose i from 1 to N - 1 such that A[i] <= A[i + 1] and then in 13 min read Maximum element in a sorted and rotated array Given a sorted array arr[] (may contain duplicates) of size n that is rotated at some unknown point, the task is to find the maximum element in it. Examples: Input: arr[] = {5, 6, 1, 2, 3, 4}Output: 6Explanation: 6 is the maximum element present in the array.Input: arr[] = {3, 2, 2, 2}Output: 3Expla 7 min read Third largest element in an array of distinct elements Given an array of n integers, the task is to find the third largest element. All the elements in the array are distinct integers. Examples : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Input: arr[] = { 11 min read Maximize elements using another array Given two arrays a[] and b[] of size n. The task is to maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first arr 15 min read Find the largest after deleting the given elements Given an array of integers, find the largest number after deleting the given elements. In case of repeating elements, delete one instance for every instance of the element present in the array containing the elements to be deleted. Examples: Input : array[] = { 5, 12, 33, 4, 56, 12, 20 } del[] = { 1 6 min read Largest Non-Repeating Element Given an array arr[] of size N, the task is to find the largest non-repeating element present in the given array. If no such element exists, then print -1. Examples: Input: arr[] = { 3, 1, 8, 8, 4 } Output: 4 Explanation: Non-repeating elements of the given array are { 1, 3, 4 } Therefore, the large 11 min read Like