AlgoInsight

DSA problem

Find the Largest Element in an Array

EasyArraysPattern: Array TraversalbeginnerFree

Given an array of N integers, find and return the largest element.

This is the most fundamental array problem. Initialize a variable max with the first element, then iterate through the rest of the array. If any element is greater than max, update max. At the end, max holds the largest element.

It runs in O(N) time, and this is provably optimal - you must look at every element at least once, since any unseen element could be the largest. This "scan and track" pattern is the basis for finding minimums, maximums, and many other aggregate values in a single pass.

Examples

Example 1
Input: nums = [3, 7, 2, 9, 1, 5]
Output: 9
Explanation: 9 is the biggest value in the array, so it is returned.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

array

Browse all DSA problems