Introduction
This blog will learn about one of the most basic programming problems, which is to count the total number of duplicate elements in an array. In this problem, we are given an array that contains some duplicate elements. Now we need to count the total duplicate elements in the array.
Now there are two ways to solve this problem using c programming. The first one is the simple approach in which we directly use two loops for iterating through the array and keep a count of the duplicate elements. Whenever we find a duplicate element, we increase the count by one and then print the total count of the duplicate number in the array to the output.
The second method, though, uses the same logic but divides the program's implementation into two functions. One is the main function in which we take the array elements like input and make a function call to another function which counts the total number of duplicate elements in the array and returns the total count to the main function. The main function then outputs the result.
So, let's begin with discussing both approaches one by one and see their implementation. If you're unfamiliar with the arrays, you can visit here to learn about them.
Also Read, Binary to Hex Converter, Fibonacci Series in Java, and C Static Function.
Simple approach
In his approach, we have to traverse through the array using a for loop, and then for each element in the array, we use another array to count any duplicate element on its right. Whenever we find a duplicate element, we increase the count by one and break the inner loop.
Let’s see the algorithm for this approach-
Algorithm
- Take the size of the array as the user input.
- Declare an array of the asked size
- Create a count variable to store the count of the duplicate elements in the array and initialize it to zero.
- Use a for loop to fill the elements into the array from the user input.
- Now, use a for loop to iterate from the first element to the last element of the array.
- For each element of the array used in the previous for loop, check if there is any duplicate element on its right using another for a loop. If yes, increase the count by one, and break this for a loop.
- Finally, print the value of the count on the output.
Implementation
Let’s see the implementation of the method and try it on the online compiler.
#include <stdio.h>
#include <conio.h>
int main()
{
//declare array and initialize the variables
int arr[10000],i,j,n,count=0 ;
printf("Enter the size of the array- ");
scanf("%d", &n);
printf("Enter elements in the array- ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
//loops to iterate through the array
for(i=0; i<n; i++)
{
for(j = i + 1; j < n; j++)
{
//Condition to check if the element is duplicate or not
if(arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("Total duplicate numbers in the array- %d",count);
return 0;
}
Output
Enter the size of the array- 6 Enter elements in the array- 1 3 4 3 6 4 Total duplicate numbers in the array- 2 |
You can also read about dynamic array in c, and Tribonacci Series