In your example consider the indexes of the 7: they are 10, 11, 12.
The first print happens when i=10 and j=10 because your are indexing numbers[10] and numbers[11].
At the next iteration of the inner loop you have i=10 and j=11 and the condition is verified again by the elements numbers[10] and numbers[12].
Finally the third condition happens when i=11 and j=11 because you are referring to numbers[11] and numbers[12].
All this becomes clear if you print your indexes when the condition happens. For example you can substitute your printf with this:
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
which prints:
Number 1 has duplicate values with indexes i=0, j=0
Number 2 has duplicate values with indexes i=2, j=2
Number 2 has duplicate values with indexes i=2, j=3
Number 2 has duplicate values with indexes i=3, j=3
Number 6 has duplicate values with indexes i=8, j=8
Number 7 has duplicate values with indexes i=10, j=10
Number 7 has duplicate values with indexes i=10, j=11
Number 7 has duplicate values with indexes i=11, j=11
If your values are ordered you can do something like this:
#include<stdio.h>
int main()
{
int numbers[15]={1,1,2,2,2,3,4,5,6,6,7,7,7,8,9},i,j;
int last_dup = -1;
for(i=0;i<15;i++)
{
for(j=i+1;j<14;j++)
{
if(numbers[i]==numbers[j] && numbers[i]!=last_dup)
{
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
last_dup = numbers[i];
break;
}
}
}
return 0;
}
I initialized to -1 assuming your numbers are all positive. If not you can save the index of the last duplicate and reference using in the check numbers[last_dup_idx] (be careful to initialize the value properly and not go out the length of the array). I also added a break: since you already found a duplicate there is no need to continue the second loop.
If your array is not ordered, you can create an additional array saving the duplicates elements every time you find one and then iterate through this array to check.
FOR NOT ORDERED ARRAYS
#include<stdio.h>
int main()
{
int numbers[15]={1,1,2,2,2,3,4,5,6,6,7,7,7,8,9},i,j;
int dups [7];//at most 15/2 duplicates
char dup_idx=0;
char found_new_duplicate; //initialized later
for(i=0;i<15;i++)
{
for(j=i+1;j<14;j++)
{
if(numbers[i]==numbers[j])
{
found_new_duplicate = 1;
//check in the duplicates array
for (int kk=0; kk<=dup_idx; kk++)
{
if (dups[kk]==numbers[i] && !(i==0 && j==1))
{
found_new_duplicate = 0;
break;
}
}
if (found_new_duplicate)
{
dups[dup_idx] = numbers[i]; //insert the new duplicate in the duplicates array
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
printf("New duplicate %d\n", dups[dup_idx]);
dup_idx++;//increase the index
break;
}
}
}
}
for (int kk=0; kk< dup_idx; kk++)
printf("%d\n", dups[kk]);
return 0;
}