1.
Introduction
2.
Simple approach
2.1.
Algorithm
2.2.
Implementation
3.
Function-based approach
3.1.
Implementation
4.
FAQs
5.
Key takeaways
Last Updated: Mar 27, 2024
Easy

# Program to Count Total Duplicate Elements in an Array

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## 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.

## 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

You can also read about dynamic array in c, and Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Function-based approach

This method is also very similar to the above method. But the only difference is that instead of doing all the calculations to count the number of duplicate elements in the array in the simple main function, it divides the implementation into two separate functions.

First, the driver function takes the size of the array and elements of the array as user input. The second function receives the array and its size as an argument. It then uses the same logic as the first approach to counting the total duplicate numbers in the array, returns the result to the first function, and then displays it on the output device.

Let's have a look at the implementation of this approach-

### Implementation

``````//Function to count the number of duplicates in the array.
#include <stdio.h>
int countduplicates(int arr[], int n)
{
int i, j, count=0;
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;
}
}
}
return count;
}
int main()
{
//declare array and initialize the variables
int arr[10000],n;
printf("Enter the size of the array- ");
scanf("%d", &n);
printf("Enter elements in the array- ");
for(int i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
//Function call to the function that will count the number of the duplicates in the array.
int totalduplicates = countduplicates(arr,n);
printf("Total duplicate numbers in the array- %d",totalduplicates);
return 0;
}``````

Output-

You can practice it by yourself with the help of online C compiler.

So that was all about the blog, and it's time to move on to the FAQs.

## FAQs

1. Can we change the size of the array?
No, we can't change the size of an array at runtime. If we declare the size of the array once, we can't change it.
2. Can we declare an array without specifying its size?
No, we can't declare an array without specifying its size. If we try to do so, we will get a compilation error.
3. What will happen if we do not initialize an array?
If we don't initialize an array, it will take the default values depending on its datatype.
4. What is the time complexity of insert operation on an array?
The time complexity of insert operation on an array is O(N).
5. Is ArrayList in java is static in size?
No ArrayList in java is not static, but it is dynamic in size, which means we modify size even after its declaration.

## Key takeaways

This article extensively discussed a programming problem that counts the total number of duplicates in an array. We discussed two approaches for this, first one was a simple approach, whereas the second approach was to divide the whole implementation into two separate functions.

Check out the following problems -

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available; look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass