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


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int arr[100];
  7. int i, j, n, count = 0;
  8.  
  9. // Readinng size of the array
  10. cout<<"Enter size of the array : ";
  11. cin>>n;
  12.  
  13. //Reading elements of array
  14. cout<<"Enter elements in array : ";
  15. for(i=0; i<n; i++)
  16. {
  17. cin>>arr[i];
  18. }
  19. //Find all duplicate elements in array
  20. for(i=0; i<n; i++)
  21. {
  22. for(j=i+1; j<n; j++)
  23. {
  24. // If duplicate element found then increment count by 1
  25. if(arr[i] == arr[j])
  26. {
  27. count++;
  28. break;
  29. }
  30. }
  31. }
  32. cout<<"\nTotal number of duplicate elements found in array: "<<count;
  33.  
  34. return 0;
  35. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts