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

  • برمجة سي شارب
  • برمجة

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

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

الأجوبة

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");
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...