CategoryC Program

C Program to find the largest element in an array

Learn how to find the largest element in an array in C using a single traversal, with source code, examples, a dry run, and complexity analysis.

To find the largest element in an array, compare every element with the largest value found so far. Whenever the current element is greater, it becomes the new largest value.

For example, in the array:

12 -4 27 8 19

the largest element is 27.

C Program to Find the Largest Array Element

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int array[MAX_SIZE];
    int size;

    printf("Enter the number of elements: ");
    if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
        printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
        return 1;
    }

    printf("Enter %d elements:\n", size);
    for (int index = 0; index < size; index++) {
        if (scanf("%d", &array[index]) != 1) {
            printf("Invalid array element.\n");
            return 1;
        }
    }

    int largest = array[0];

    for (int index = 1; index < size; index++) {
        if (array[index] > largest) {
            largest = array[index];
        }
    }

    printf("Largest element = %d\n", largest);

    return 0;
}

Sample Output

Enter the number of elements: 5
Enter 5 elements:
12 -4 27 8 19
Largest element = 27

How the Program Works

  1. The program reads the array size and checks that it is between 1 and MAX_SIZE.
  2. A loop stores the entered integers in the array.
  3. largest is initialized with array[0], the first element.
  4. The comparison loop starts at index 1 because the first element is already stored in largest.
  5. If array[index] is greater than largest, the program updates largest.
  6. After every element has been checked, largest contains the maximum value in the array.

Here is a dry run for 12, -4, 27, 8, 19:

Current elementLargest before comparisonLargest after comparison
-41212
271227
82727
192727

Why Initialize largest with the First Element?

Initializing largest to 0 gives an incorrect result when every array element is negative. For example, the largest element in -9, -3, -12 is -3, but a variable initialized to 0 would never be updated.

Using array[0] guarantees that the initial value comes from the array itself, so the algorithm works with positive numbers, negative numbers, zero, and mixtures of them.

Using a Function to Find the Largest Element

We can move the search into a reusable function:

#include <stdio.h>

int findLargest(const int array[], int size) {
    int largest = array[0];

    for (int index = 1; index < size; index++) {
        if (array[index] > largest) {
            largest = array[index];
        }
    }

    return largest;
}

int main(void) {
    int numbers[] = {-11, -5, -20, -2};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    printf("Largest element = %d\n", findLargest(numbers, size));

    return 0;
}

Output

Largest element = -2

The const keyword shows that findLargest() only reads the array and does not modify its elements. The function expects size to be at least 1 because it uses the first element as its initial value.

Time and Space Complexity

  • Time complexity: O(n), because each of the n elements is examined once.
  • Extra space complexity: O(1), because the search uses only a variable for the current largest value and a loop index.

Common Mistakes

  • Initializing largest to 0, which fails for arrays containing only negative numbers.
  • Starting the comparison loop at index 0 unnecessarily after assigning array[0] to largest.
  • Using < instead of >, which finds the smallest element rather than the largest.
  • Reading an empty array and then trying to access array[0].
  • Sorting the entire array just to find its largest value. Sorting takes more work than the single traversal used here.

A single left-to-right traversal is sufficient to find the largest element, and the original order of the array remains unchanged.