Program to count the total number of duplicate elements in an array.

Here, we have a basic program example to count the duplicate elements in an array using different languages. This program is created in c language, c++, Java, and Python.

Code to count duplicate elements in an array in C language

#include <stdio.h>
void  main()
{
    int i,j,n, count=0;
       printf("\n\nEnter the size of the array\n");
       scanf("%d",&n);
   int arr[n];
    printf("Input elements in the array :\n");
    for(i=0; i<n; i++)
    {
	    printf("element - %d : ",i);
        scanf("%d", &arr[i]);
    }

     for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr[i] == arr[j])
            {
                count++;
                break;
            }
        }
    }
    printf("\nTotal number of duplicate elements found in array = %d", count);
    return 0;
}

Code to count duplicate elements in an array in C++ language

#include <iostream>
using namespace std;

int main()
{
   int i,j,n,count=0;
       cout<<"\nEnter the size of the array\n";
       cin>>n;
   int arr[n];
    cout<<"Input elements in the array :\n";
    for(i=0; i<n; i++)
    {
	    cout<<"element " << i <<" - ";
        cin>>arr[i];
    }
     for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr[i] == arr[j])
            {
                count++;
                break;
            }
        }
    }
    cout<<"\nTotal number of duplicate elements found in array = "<<count;
    return 0;
}

Code to count duplicate elements in an array in Python language

a=[]
count=0
n=int(input("Enter the size of the array: "))
print("Input elements in the array :")
for i in range(0,n):
    l=int(input())
    a.append(l)

for i in range(0,n):
    for j in range(i+1,n):
        if(a[i]==a[j]):
            count=count+1
            break
print("Total number of duplicate elements found in array = ",count)

Code to count duplicate elements in an array in Java language

import java.util.*;
public class arrays
{
  public static void main(String[] args)
   {
     int n,count=0;
     Scanner sc = new Scanner(System.in);
     System.out.print("Enter size of the array : ");
     n=sc.nextInt();

    int[] arr = new int[n];

    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<n; i++)
      {
       arr[i]=sc.nextInt();
       }
     for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(arr[i] == arr[j])
            {
                count++;
                break;
            }
        }
    }

    System.out.println("\nTotal number of duplicate elements found in array = "+ count);

   }
}