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:
- They contain the same number of elements.
- 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
areEqualbegins as1, meaning the arrays are assumed equal until a difference is found.- The program reads both sizes and both arrays.
- If the sizes differ, the arrays cannot be equal, so
areEqualbecomes0. - If the sizes match, a loop compares elements at the same index.
- The first mismatch sets
areEqualto0and stops the loop withbreak. - If no mismatch is found, the flag remains
1and the arrays are equal.
Here is a dry run for two equal arrays:
| Index | First array | Second array | Result |
|---|---|---|---|
| 0 | 4 | 4 | Continue |
| 1 | 7 | 7 | Continue |
| 2 | 2 | 2 | Continue |
| 3 | 9 | 9 | All 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 containnelements 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 useO(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 == secondand 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.