0
0
CppProgramBeginner · 2 min read

C++ Program to Find Largest Element in Array

To find the largest element in an array in C++, use a loop to compare each element with a variable holding the current largest value, updating it when a bigger element is found, like for (int i = 1; i < n; i++) if (arr[i] > largest) largest = arr[i];.
📋

Examples

Inputarr = {3, 5, 1, 9, 2}
Output9
Inputarr = {10, 10, 10}
Output10
Inputarr = {-5, -2, -9, -1}
Output-1
🧠

How to Think About It

To find the largest number, start by assuming the first element is the largest. Then check each other element one by one. If you find a number bigger than the current largest, update your largest number to that. After checking all, the largest number you have is the biggest in the array.
📐

Algorithm

1
Start with the first element as the largest.
2
Go through each element in the array from the second to the last.
3
Compare the current element with the largest found so far.
4
If the current element is bigger, update the largest.
5
After checking all elements, return the largest value.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int largest = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] > largest) {
            largest = arr[i];
        }
    }

    cout << "Largest element is: " << largest << endl;
    return 0;
}
Output
Largest element is: 9
🔍

Dry Run

Let's trace the array {3, 5, 1, 9, 2} through the code

1

Initialize largest

largest = 3 (first element)

2

Compare with second element

arr[1] = 5 > largest (3), update largest = 5

3

Compare with third element

arr[2] = 1 <= largest (5), no change

4

Compare with fourth element

arr[3] = 9 > largest (5), update largest = 9

5

Compare with fifth element

arr[4] = 2 <= largest (9), no change

6

End of loop

largest = 9 is the largest element

IterationCurrent ElementLargest So Far
155
215
399
429
💡

Why This Works

Step 1: Start with first element

We assume the first element is the largest because we need a starting point to compare others.

Step 2: Compare each element

We check every other element to see if it is bigger than the current largest.

Step 3: Update largest when needed

If a bigger element is found, we update the largest variable to hold this new value.

Step 4: Result after loop

After checking all elements, the largest variable holds the biggest number in the array.

🔄

Alternative Approaches

Using std::max_element from <algorithm>
cpp
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int largest = *max_element(arr, arr + n);
    cout << "Largest element is: " << largest << endl;
    return 0;
}
This method is shorter and uses built-in functions but requires including <algorithm>.
Sorting the array and picking last element
cpp
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n);
    cout << "Largest element is: " << arr[n - 1] << endl;
    return 0;
}
Sorting is less efficient for just finding the largest but useful if you need sorted data.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so the time grows linearly with the array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The simple loop is fastest for this task; using sorting is slower (O(n log n)) and built-in max_element is efficient and clean.

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Finding largest quickly with minimal code
std::max_elementO(n)O(1)Cleaner code using standard library
SortingO(n log n)O(1)When sorted data is also needed
💡
Always initialize the largest variable with the first element of the array before looping.
⚠️
Beginners often forget to start the loop from the second element, causing incorrect comparisons.