CategoryC Program

C Program to check whether two arrays are equal

Learn how to check whether two C arrays have the same length and matching elements at every index, with validated code, examples, and complexity.

Two arrays are equal when both of these conditions are true:

  1. They contain the same number of elements.
  2. The values at every corresponding index are equal.

For example:

First array:  4 7 2 9
Second array: 4 7 2 9

These arrays are equal because they have the same length and every pair of elements at matching indices is equal.

C Program to Check Whether Two Arrays Are Equal

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int first[MAX_SIZE];
    int second[MAX_SIZE];
    int firstSize;
    int secondSize;
    int areEqual = 1;

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

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

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

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

    if (firstSize != secondSize) {
        areEqual = 0;
    } else {
        for (int index = 0; index < firstSize; index++) {
            if (first[index] != second[index]) {
                areEqual = 0;
                break;
            }
        }
    }

    if (areEqual) {
        printf("The arrays are equal.\n");
    } else {
        printf("The arrays are not equal.\n");
    }

    return 0;
}

Sample Output for Equal Arrays

Enter the number of elements in the first array: 4
Enter 4 elements for the first array:
4 7 2 9
Enter the number of elements in the second array: 4
Enter 4 elements for the second array:
4 7 2 9
The arrays are equal.

How the Program Works

  1. areEqual begins as 1, meaning the arrays are assumed equal until a difference is found.
  2. The program reads both sizes and both arrays.
  3. If the sizes differ, the arrays cannot be equal, so areEqual becomes 0.
  4. If the sizes match, a loop compares elements at the same index.
  5. The first mismatch sets areEqual to 0 and stops the loop with break.
  6. If no mismatch is found, the flag remains 1 and the arrays are equal.

Here is a dry run for two equal arrays:

IndexFirst arraySecond arrayResult
044Continue
177Continue
222Continue
399All elements match

Why Must the Lengths Match?

Arrays with different lengths cannot be equal, even when all elements of the shorter array match the beginning of the longer array:

First array:  1 2 3
Second array: 1 2 3 4

The additional 4 makes the arrays different. Checking the lengths first also prevents comparisons beyond the end of the shorter array.

Why Does Element Order Matter?

Array equality compares corresponding positions. These arrays contain the same values but are not equal:

First array:  1 2 3
Second array: 3 2 1

At index 0, the values 1 and 3 differ. Therefore, the arrays fail the equality test immediately.

If a problem asks whether arrays contain the same values regardless of order, it is asking for multiset or set equality, which requires different logic.

What About Duplicate Values?

Duplicates are handled naturally as long as they occur at the same indices:

First array:  5 2 5 5
Second array: 5 2 5 5

These arrays are equal. If one duplicate is missing, added, or moved to another index, they are not equal.

Mismatch Example

Enter the number of elements in the first array: 4
Enter 4 elements for the first array:
4 7 2 9
Enter the number of elements in the second array: 4
Enter 4 elements for the second array:
4 7 8 9
The arrays are not equal.

The loop stops at index 2, where 2 differs from 8. Later positions do not need to be checked after equality has already failed.

Does It Work with Negative Numbers and Zero?

Yes. Direct integer comparison supports positive values, negative values, and zero:

First array:  -3 0 8
Second array: -3 0 8
Result: Equal

Can == Compare Entire Arrays in C?

No. Writing first == second compares the arrays' starting addresses after they decay to pointers in an expression; it does not compare their elements.

Likewise, first = second cannot copy one array to another. C array elements must be compared individually with a loop or an appropriate library function.

For integer arrays, an element-by-element loop is explicit and avoids representation-related assumptions.

Time and Space Complexity

  • Best-case comparison time: O(1) when the sizes differ or the first elements do not match.
  • Worst-case comparison time: O(n) when equal arrays contain n elements or the mismatch is at the last index.
  • Extra space complexity: O(1) for the comparison because only a flag and loop index are needed. The two input arrays themselves use O(n + m) space.

Common Mistakes

  • Comparing only the array sizes without checking the elements.
  • Comparing elements without first checking that the sizes match.
  • Ignoring element order when ordered equality is required.
  • Writing first == second and expecting an element-by-element comparison.
  • Continuing through the array after a mismatch when no more comparisons are needed.
  • Using index <= size, which accesses one position beyond the valid array.

By checking the lengths first and then comparing corresponding elements, the program determines ordered array equality safely and efficiently.