﻿ C# Sharp Exercises: Count a total number of duplicate elements in an array - w3resource

# C# Sharp Exercises: Count a total number of duplicate elements in an array

## C# Sharp Array: Exercise-5 with Solution

Write a program in C# Sharp to count a total number of duplicate elements in an array.

Sample Solution:-

C# Sharp Code:

``````using System;
public class Exercise5
{
public static void Main()
{
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int[] arr3 = new int[100];
int s1, s2,mm=1,ctr=0;
int i, j;
Console.Write("\n\nCount total number of duplicate elements in an array:\n");
Console.Write("---------------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");

Console.Write("Input {0} elements in the array :\n",s1);
for(i=0;i<s1;i++)
{
Console.Write("element - {0} : ",i);
}
/*----------------- copy in other array ------------------------------------*/
for(i=0;i<s1; i++)
{
arr2[i]=arr1[i];
arr3[i]=0;
}
/*------------------- mark the elements are duplicate -------------------------*/
for(i=0;i<s1; i++)
{
for(j=0;j<s1;j++)
{
if(arr1[i]==arr2[j])
{
arr3[j]=mm;
mm++;
}
}
mm=1;
}
/*--------------- Prints the array ------------------------------------*/
for(i=0; i<s1; i++)
{
if(arr3[i]==2){ctr++;}
}
Console.Write("The number of duplicate elements is: {0} \n", ctr);

Console.Write("\n\n");
}
}
```
```

Sample Output:

```
Count total number of duplicate elements in an array:
---------------------------------------------------------
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 2
element - 2 : 4
Total number of duplicate elements found in the array is : 1
```

Flowchart:

C# Sharp Code Editor: