GastonFontenla

Untitled

Oct 26th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. bool tieneRepetidos(vector <int> v)
  7. {
  8.     ///Solución 1: Hacer for anidado
  9.     /**for(int i=0; i<v.size(); i++)
  10.     {
  11.         for(int j=i+1; j<v.size(); j++)
  12.         {
  13.             if(v[i] == v[j])
  14.             {
  15.                 return true;
  16.             }
  17.         }
  18.     }
  19.  
  20.     return false;**/
  21.     ///Solución 2: Sort y ver si hay algún par de consecutivos que sean iguales
  22.  
  23.     ///Solución 3: Hacer un vector que cuente frecuencia de valores
  24.  
  25.     vector <bool> frecuencia(10001, false);
  26.  
  27.     for(int i=0; i<v.size(); i++)
  28.     {
  29.         if(frecuencia[v[i]] == true)
  30.         {
  31.             return true;
  32.         }
  33.         frecuencia[v[i]] = true;
  34.     }
  35.  
  36.     return false;
  37. }
  38.  
  39. int main()
  40. {
  41.     /**
  42.     Tenemos N números, cada uno va desde 0 hasta 10.000
  43.     N <= 100
  44.    
  45.     ///Ejemplo input N = 8, 0 6 3 6 6 8 0 6
  46.     **/
  47.  
  48.     int n;
  49.     cin >> n;
  50.     vector <int> v(n);
  51.  
  52.     for(int i=0; i<n; i++)
  53.     {
  54.         cin >> v[i];
  55.     }
  56.  
  57.     if(tieneRepetidos(v) == true)
  58.     {
  59.         cout << "Hay repetidos" << endl;
  60.     }
  61.     else
  62.     {
  63.         cout << "No hay repetidos" << endl;
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment