Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.

C# Sharp: 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 :");
	   s1 = Convert.ToInt32(Console.ReadLine()); 	   
   
       Console.Write("Input {0} elements in the array :\n",s1);
       for(i=0;i<s1;i++)
            {
	      Console.Write("element - {0} : ",i);
	     arr1[i] = Convert.ToInt32(Console.ReadLine()); 		  
	    }
/*----------------- 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:

Flowchart: Count total number of duplicate elements in an array

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to copy the elements one array into another array.
Next: Write a program in C# Sharp to print all unique elements in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.