Largest element in an Array Last Updated : 27 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : Searching Matrix DSA Arrays Basic Coding Problems Order-Statistics +2 More Practice Tags : ArraysMatrixSearching Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like