0
0
CProgramBeginner · 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, like for (int i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; }.
📋

Examples

Inputarr = {3, 5, 1, 9, 2}, n = 5
OutputLargest element is 9
Inputarr = {10, 10, 10, 10}, n = 4
OutputLargest element is 10
Inputarr = {-5, -2, -9, -1}, n = 4
OutputLargest element is -1
🧠

How to Think About It

To find the largest element, start by assuming the first element is the largest. Then, check each other element one by one. If you find an element bigger than the current largest, update your largest value. After checking all elements, the largest value 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 value found so far.
4
If the current element is bigger, update the largest value.
5
After checking all elements, return the largest value.
💻

Code

c
#include <stdio.h>

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

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

    printf("Largest element is %d\n", max);
    return 0;
}
Output
Largest element is 9
🔍

Dry Run

Let's trace the array {3, 5, 1, 9, 2} through the code to find the largest element.

1

Initialize max

max = 3 (first element)

2

Compare arr[1] = 5 with max = 3

5 > 3, so max = 5

3

Compare arr[2] = 1 with max = 5

1 > 5? No, max stays 5

4

Compare arr[3] = 9 with max = 5

9 > 5, so max = 9

5

Compare arr[4] = 2 with max = 9

2 > 9? No, max stays 9

6

End of array

Largest element found is 9

IndexCurrent ElementCurrent Max
033
155
215
399
429
💡

Why This Works

Step 1: Initialize max

We start by assuming the first element is the largest using max = arr[0] so we have a baseline to compare others.

Step 2: Compare each element

We use a loop to check each element with if (arr[i] > max) to find if there's a bigger number.

Step 3: Update max when needed

If a bigger element is found, we update max to that value, ensuring it always holds the largest found so far.

Step 4: Result after loop

After checking all elements, max contains the largest element, which we print.

🔄

Alternative Approaches

Using sorting
c
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    qsort(arr, n, sizeof(int), compare);
    printf("Largest element is %d\n", arr[n-1]);
    return 0;
}
Sorting the array and picking the last element is simple but less efficient (O(n log n)) compared to scanning (O(n)).
Using recursion
c
#include <stdio.h>

int findMax(int arr[], int n) {
    if (n == 1) return arr[0];
    int max = findMax(arr, n - 1);
    return (arr[n - 1] > max) ? arr[n - 1] : max;
}

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Largest element is %d\n", findMax(arr, n));
    return 0;
}
Recursion is elegant but uses more memory due to function calls and is less efficient for large arrays.

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

Time Complexity

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

Space Complexity

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

Which Approach is Fastest?

The simple loop approach is fastest and most memory efficient compared to sorting (O(n log n)) or recursion (extra call stack).

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Most efficient for all array sizes
SortingO(n log n)O(1)When array needs sorting anyway
RecursionO(n)O(n)Elegant but uses more memory
💡
Always initialize your largest value with the first element to avoid errors.
⚠️
A common mistake is starting max at zero, which fails if all array elements are negative.