ValerianBenchea

Numarul care se repeta de cele mai multe ori intr-un vector

Jan 18th, 2021
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int aux, n, i, j, nr, m, max, y, a[20], v[20];
  8.     cout<<"n="; cin>>n;
  9.     for(i=1; i<=n; i++)
  10.     {
  11.         cout<<"a["<<i<<"]=";
  12.         cin>>a[i];
  13.     }
  14.     aux=-1;
  15.     max=a[1];
  16.     for(i=2; i<=n; i++)
  17.        if(max<a[i])
  18.             max=a[i];
  19.     m=max;
  20.     for(i=1; i<=m; i++)
  21.         v[i]=0;
  22.     for(i=1; i<n; i++)
  23.     {
  24.         nr=1;
  25.         y=a[i];
  26.         if(y!=aux)
  27.             for(j=i+1;j<=n;j++)
  28.                 if(y==a[j])
  29.                 {
  30.                     nr++;
  31.                     a[j]=aux;
  32.                 }
  33.         v[y]=nr;
  34.     }
  35.     max=v[1];
  36.     j=1;
  37.     for(i=2; i<=m; i++)
  38.         if(max<v[i])
  39.         {
  40.             max=v[i];
  41.             j=i;
  42.         }
  43.     cout<<"Numarul "<<j<<" se repeta de "<<max<<" ori";
  44.     return 0;
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment