0
0
CProgramBeginner · 2 min read

C Program to Find Largest Element in an 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 = {5, 3, 9, 1, 6}
Output9
Inputarr = {-2, -8, -1, -5}
Output-1
Inputarr = {7}
Output7
🧠

How to Think About It

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

Algorithm

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

Code

c
#include <stdio.h>

int main() {
    int arr[] = {5, 3, 9, 1, 6};
    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];
        }
    }

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

Dry Run

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

1

Initialize largest

largest = 5 (first element)

2

Compare with second element

arr[1] = 3; 3 > 5? No; largest stays 5

3

Compare with third element

arr[2] = 9; 9 > 5? Yes; largest updated to 9

4

Compare with fourth element

arr[3] = 1; 1 > 9? No; largest stays 9

5

Compare with fifth element

arr[4] = 6; 6 > 9? No; largest stays 9

6

End of loop

largest = 9 is the largest element

IterationCurrent ElementLargest
135
299
319
469
💡

Why This Works

Step 1: Start with first element

We assume the first element is the largest to have a starting point for comparison.

Step 2: Compare each element

Each element is checked to see if it is bigger than the current largest value.

Step 3: Update largest if 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 a function to find largest
c
#include <stdio.h>

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

int main() {
    int arr[] = {5, 3, 9, 1, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Largest element is %d\n", findLargest(arr, n));
    return 0;
}
This approach improves code reuse by separating logic into a function.
Using sorting to find largest
c
#include <stdio.h>
#include <stdlib.h>

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

int main() {
    int arr[] = {5, 3, 9, 1, 6};
    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 works but is less efficient for just finding the largest.

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

Time Complexity

The program checks each element once in a single loop, so it runs in linear time O(n), where n is the number of elements.

Space Complexity

It uses only a few extra variables, so the space used is constant O(1), regardless of input size.

Which Approach is Fastest?

The direct loop method is fastest and simplest. Sorting is slower (O(n log n)) and uses more resources, so it's not ideal just to find the largest.

ApproachTimeSpaceBest For
Direct loopO(n)O(1)Finding largest quickly with minimal memory
Function abstractionO(n)O(1)Code reuse and clarity
SortingO(n log n)O(1)When array needs to be sorted anyway
💡
Always initialize the largest variable with the first element of the array before looping.
⚠️
A common mistake is starting the largest variable at zero, which fails if all array elements are negative.