C Program to Count Total Duplicate Elements in an Array

How to write a C Program to Count Total Duplicate Elements in an Array?. Before counting duplicate elements in an array, please refer to Array in C article to know the Array size, index position, etc.

C Program to Count Total Duplicate Elements in an Array Example

This C program asks the user to enter Array Size and array elements. Next, it is going to count the total number of duplicate elements present in this array using For Loop.

#include <stdio.h>

int main()
{
int arr[10], i, j, Size, Count = 0;

printf("\n Please Enter Number of elements in an array : ");
scanf("%d", &Size);

printf("\n Please Enter %d elements of an Array : ", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}

for (i = 0; i < Size; i++)
{
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
Count++;
break;
}
}
}

printf("\n Total Number of Duplicate Elements in this Array = %d ", Count);

return 0;
}
C Program to Count Total Duplicate Elements in an Array 1

In this Program, We declared 1 One Dimensional Arrays arr[] of size 10 and also declared i to iterate the Array elements. The below printf statement asks the User to enter the C Array arr[] size (Number of elements an Array can hold). And, scanf statement will assign the user entered values to Size variable.

printf("\n Please Enter Number of elements in an array  :   ");
scanf("%d", &Size);

Below C For loop will help to iterate each cell present in the arr[5] array. Condition (i < Size) in the for loop ensure that the compiler to not exceed the array limit. The C language scanf statement inside the for loop will store the user entered values in every individual array element such as arr[0], arr[1], arr[2], arr[3], arr[4]

for (i = 0; i < Size; i++)
{
    	scanf("%d", &arr[i]);
}

In the next line, We have one more for loop. It is to iterate each element in an array. C If Statement to check for the duplicates

for (i = 0; i < Size; i++)
{
	for(j = i + 1; j < Size; j++)
	{
   		if(arr[i] == arr[j])
   		{
  			Count++;
			break;
		}
	}
}

User inserted values are: a[5] = {10, 20, 10, 30, 20}

First For Loop – First Iteration: for(i = 0; i < 5; 0++)
The condition (0 < 5) is True.

Second For Loop – First Iteration: for(j = 0 + 1; 1 < 5; 1++)
The condition (1 < 5) is True. So, it will start executing the statements inside the loop
if(arr[i] == arr[j])
if(10 == 20) – Condition is False

Second For Loop – Second Iteration: for(j = 2; 2 < 5; 2++)
The condition (2 < 5) is True. So, it will start executing the statements inside the loop
if(arr[i] == arr[j])
if(10 == 10) – Condition is True

The count will increment to 1. Do the same for remaining iterations.

C Program to Count Total Duplicate Elements in an Array Example 2

This program to count duplicate array elements is same as above, but this time we separated the logic using Functions concept. Refer to the C Function article.

#include <stdio.h>

int Total_Duplicates(int arr[], int Size);

int main()
{
int arr[10], i, Size, Count = 0;

printf("\n Please Enter Number of elements in an array : ");
scanf("%d", &Size);

printf("\n Please Enter %d elements of an Array : ", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}

Count = Total_Duplicates(arr, Size);

printf("\n Total Number of Duplicate Elements in this Array = %d ", Count);

return 0;
}
int Total_Duplicates(int arr[], int Size)
{
int i, j, Count = 0;

for (i = 0; i < Size; i++)
{
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
Count++;
break;
}
}
}
return Count;
}
Please Enter Number of elements in an array  :   10

 Please Enter 10 elements of an Array  :  1 2 3 4 1 2 5 6 5 9

 Total Number of Duplicate Elements in this Array  =  3

Also Read