Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- bool tieneRepetidos(vector <int> v)
- {
- ///Solución 1: Hacer for anidado
- /**for(int i=0; i<v.size(); i++)
- {
- for(int j=i+1; j<v.size(); j++)
- {
- if(v[i] == v[j])
- {
- return true;
- }
- }
- }
- return false;**/
- ///Solución 2: Sort y ver si hay algún par de consecutivos que sean iguales
- ///Solución 3: Hacer un vector que cuente frecuencia de valores
- vector <bool> frecuencia(10001, false);
- for(int i=0; i<v.size(); i++)
- {
- if(frecuencia[v[i]] == true)
- {
- return true;
- }
- frecuencia[v[i]] = true;
- }
- return false;
- }
- int main()
- {
- /**
- Tenemos N números, cada uno va desde 0 hasta 10.000
- N <= 100
- ///Ejemplo input N = 8, 0 6 3 6 6 8 0 6
- **/
- int n;
- cin >> n;
- vector <int> v(n);
- for(int i=0; i<n; i++)
- {
- cin >> v[i];
- }
- if(tieneRepetidos(v) == true)
- {
- cout << "Hay repetidos" << endl;
- }
- else
- {
- cout << "No hay repetidos" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment