C program count total duplicate elements in an array

Write a C program to read elements in an array from user and count total number of duplicate elements in array. C program to find all duplicate elements in an unsorted array. How to list all duplicate elements in an unsorted array using loop in C programming.

Example

Input

Input array elements: 1, 10, 20, 1, 25, 1, 10, 30, 25, 1

Output

Total number of duplicate elements = 5

Required knowledge

Basic C programming, If else, For loop, Nested loop, Array

Must know - Program to read and print array elements

Logic to count total duplicate elements in array

Below is the step by step descriptive logic to count total duplicate elements in an array.

  1. Read elements in an array, say arr contains all array elements.
  2. Initialize another variable to store duplicate count, say count = 0.
  3. To count total duplicate elements in a given array we need two loops. Run an outer loop loop from 0 to N (where N is size of the array). The loop structure must look like for(i=0; i<N; i++). This loop is used to select each element of array and check next subsequent elements for duplicates elements using another nested loop.
  4. Run another inner loop to find first duplicate of current element i.e. arr[i]. Run an inner loop from i + 1 to N, the loop structure must look like for(j=i+1; j<N; j++). Now, why run a loop from i + 1. Because we need to search for duplicate elements in next subsequent from current element and arr[i] represents the current element.
  5. Inside the inner loop check for duplicate element. If a duplicate element is found then increment duplicate count. Which is if(arr[i] == arr[j]) then, count++. Also terminate inner loop if a duplicate element is found.

Read more - Program to print all unique elements in array

Program to count total duplicate elements in an array

/*
 * C program to count total number of duplicate elements in an array
 */

#include <stdio.h>

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

    /* Input size of the array */
    printf("Enter size of the array : ");
    scanf("%d", &n);

    /* Input elements in the array */
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*
     * Find all duplicate elements in array
     */
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            /* If duplicate found then increment count by 1 */
            if(arr[i] == arr[j])
            {
                count++;
                break;
            }
        }
    }

    printf("\nTotal number of duplicate elements found in array = %d", count);

    return 0;
}

Output

Enter size of the array : 10
Enter elements in array : 1 10 20 1 25 1 10 30 25 1

Total number of duplicate elements found in array = 5

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>