# 1. Introduction

Arrays are fundamental data structures in programming. Often, one needs to find out if there are any duplicate elements present in the array. In this guide, we will walk you through a C program that counts the total number of duplicate elements in an array, followed by a comprehensive breakdown of the program.

# 2. Program Overview

The C program performs the following tasks:

1. Ask the user for the size of the array and its elements.

2. Traverse the array to check for duplicate elements.

3. Count and display the number of duplicate elements.

# 3. Code Program

``````#include <stdio.h>

int main() {
int arr[100], n, i, j, count = 0;

// Asking user to input the number of elements
printf("Enter the number of elements in array: ");
scanf("%d", &n);

printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Checking for duplicate elements
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
count++;
break;
}
}
}

// Printing the count of duplicate elements
printf("Total number of duplicate elements found: %d", count);

return 0;
}
``````

### Output:

```Enter the number of elements in array: 6
Enter 6 integers:
10
10
20
20
30
30
Total number of duplicate elements found: 3
```

# 4. Step By Step Explanation

1. Variable Declaration:

- arr[100] is the array that will store the elements.

- n represents the number of elements in the array.

- count is used to store the number of duplicate elements.

2. Getting Input from the User:

- The program prompts the user to specify the number of elements and then the elements themselves.

3. Checking for Duplicate Elements:

- A nested loop is used to compare each element with the rest.

- The outer loop, controlled by variable i, picks each element of the array one by one.

- The inner loop, controlled by j, compares the element picked by the outer loop with each of the succeeding elements. If a match is found (i.e., if arr[i] == arr[j]), then we have a duplicate.

- On finding a duplicate, we increment our count and break out of the inner loop to avoid counting the same duplicate multiple times.

4. Displaying the Result:

- Finally, the program prints the count of duplicate elements found.

With this program, you can quickly identify the number of duplicates in an array. However, if the array size is very large, there are more efficient algorithms to consider, which can help reduce the computational complexity.