C Program to find the first duplicate element in an array
Learn how to find the first array element that appears again later in C, with validated code, examples, a dry run, edge cases, and complexity analysis.
In this article, the first duplicate element means the earliest element in array order that appears again at a later index.
For example:
Array: 5 3 8 5 3 9
The first duplicate element is 5 because the value at index 0 appears again at index 3. The value 3 also repeats, but its first occurrence is at the later index 1.
C Program to Find the First Duplicate Element
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int duplicateValue = 0;
int firstIndex = -1;
int repeatedIndex = -1;
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;
}
}
for (int current = 0; current < size - 1; current++) {
for (int next = current + 1; next < size; next++) {
if (array[current] == array[next]) {
duplicateValue = array[current];
firstIndex = current;
repeatedIndex = next;
break;
}
}
if (firstIndex != -1) {
break;
}
}
if (firstIndex == -1) {
printf("The array has no duplicate elements.\n");
} else {
printf("First duplicate element = %d\n", duplicateValue);
printf("It first appears at index %d and repeats at index %d.\n",
firstIndex,
repeatedIndex);
}
return 0;
}
Sample Output
Enter the number of elements: 6
Enter 6 elements:
5 3 8 5 3 9
First duplicate element = 5
It first appears at index 0 and repeats at index 3.
How the Program Works
firstIndexandrepeatedIndexstart at-1, which means no duplicate has been found.- The outer loop selects elements from left to right.
- The inner loop compares the selected element with every value after it.
- When two values match, the program saves the value and both indices.
- The inner
breakstops the current comparison loop. - The outer
breakstops the search because the earliest repeating element has been found. - If
firstIndexremains-1, every array element is unique.
Here is a shortened dry run for 5, 3, 8, 5, 3, 9:
| Current index | Next index | Comparison | Result |
|---|---|---|---|
| 0 | 1 | 5 != 3 | Continue |
| 0 | 2 | 5 != 8 | Continue |
| 0 | 3 | 5 == 5 | Save 5 and stop |
The program does not need to check the element at index 1 because a duplicate for the earlier element at index 0 has already been found.
What Does “First Duplicate” Mean?
The phrase can have two interpretations:
- Earliest element that repeats later: Choose the repeated value whose first occurrence has the smallest index. This is the definition used by the complete program.
- First repeated occurrence encountered: Scan from left to right and choose the first value that has already appeared earlier.
These definitions can produce different answers. Consider:
Array: 2 1 3 1 2
- The earliest element that repeats later is
2, because its first occurrence is at index0. - The first repeated occurrence encountered is
1, because its second occurrence at index3appears before the second2at index4.
Always confirm which meaning a problem expects. This article uses the first definition, which is also commonly called the first repeating element.
How to Find the First Repeated Occurrence Instead
To use the second definition, make the current index move from left to right and compare it only with earlier elements:
int found = 0;
for (int current = 1; current < size && !found; current++) {
for (int previous = 0; previous < current; previous++) {
if (array[current] == array[previous]) {
printf("First repeated occurrence = %d\n", array[current]);
found = 1;
break;
}
}
}
The loop structure changes which occurrence is prioritized, even though both approaches compare array elements for equality.
What If There Are No Duplicates?
When all elements occur once, no comparison succeeds and firstIndex remains -1:
Enter the number of elements: 5
Enter 5 elements:
4 -2 7 0 11
The array has no duplicate elements.
The same result occurs for a one-element array because one value cannot have a duplicate without another element.
Does It Work with Negative Numbers and Zero?
Yes. The program uses direct integer equality, so it works with positive values, negative values, and zero. For example, the first duplicate in -3, 0, 8, -3 is -3.
Why Not Sort the Array First?
Sorting places equal values next to each other and can make duplicate detection faster. However, sorting changes the original order, so it loses the information needed to determine which element appeared first unless the original indices are stored separately.
The nested-loop solution preserves the array and requires no additional collection.
Time and Space Complexity
- Best-case time complexity:
O(1)when the first two elements are equal. - Worst-case time complexity:
O(n²)when no duplicates exist or the first qualifying duplicate requires nearly all comparisons. - Extra space complexity:
O(1)because only a few value, index, and loop variables are used.
Common Mistakes
- Not defining what “first” means before implementing the search.
- Comparing an element with itself, which would make every value appear duplicated.
- Starting the inner loop at
0instead ofcurrent + 1for this definition. - Breaking only the inner loop and then accidentally continuing the outer search.
- Sorting the array and losing its original order.
- Assuming
0can represent “not found,” even though index0is valid; use-1instead.
By checking each element against all later values and stopping at the first match, the program finds the earliest array element that occurs more than once.