Write C# program to count total duplicate elements in an array


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. using System;
  2. class Program
  3. {
  4. static void Main()
  5. {
  6. int[] arr = new int[100];
  7. int i, j, num, count = 0;
  8.  
  9. //Reads size of the array
  10. Console.WriteLine("Enter size of the array: ");
  11. num = Convert.ToInt32(Console.ReadLine());
  12.  
  13. //Reads elements in array
  14. Console.WriteLine("Enter elements in the array: ");
  15. for (i = 0; i < num; i++)
  16. {
  17. arr[i] = Convert.ToInt32(Console.ReadLine());
  18. }
  19.  
  20. //Find all duplicate elements in array
  21. for (i = 0; i < num; i++)
  22. {
  23. for (j = i + 1; j < num; j++)
  24. {
  25. // If duplicate element found then increment count by 1
  26. if (arr[i] == arr[j])
  27. {
  28. count++;
  29. break;
  30. }
  31. }
  32. }
  33.  
  34. Console.WriteLine("\n Total number of duplicate elements found in array:"+count);
  35. Console.ReadLine();
  36. }
  37.  
  38. }

Result

C# language techstudy

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.