repeated number vector program

Asked

Viewed 33 times

-1

I need to create a vector that in case see what the repeated numbers and give as output it, but if there are 2 values that have the same amount of repetition the output has to be the highest value

my code is like this for now

Dit: I’m not sure how to make the output give the highest value

ex:

entree:

6

5 5 5 3 3

output : 3

that’s what’s going on in my code and I’d need you to leave 5 but I don’t know how to do it

#include <iostream>
 
 using namespace std;

  int main() {
 

int n,i,nota ;

cin >> n;

int a[n];

for (int i = 0; i< n; i++){
  cin >> a[i];
}

 for(i = 0; i < n; i++)
  {
     for(int j = i+1; j < n; j++)
     {
         if(a[i] == a[j]){
           

           nota = a[j]; 
         
         }

 }
   } cout << nota;

    return 0;


}
  • Welcome @Davidfranca9, it would be interesting for you to tell us what your difficulties are and what is going wrong in your code so we can better help you. Your question is too vague.

  • Your if shouldn’t be > instead of ==????

  • but I’ve tried the same thing

  • From what I understand your variable nota contains the last number that has been repeated, regardless of the number of repetitions or their value. The number of repetitions matters?

  • Also not understood when it speaks of "2 vectors", its program treats a single vector.

  • and were 2 values not vectors I will correct

  • Do you have to leave the bigger one, or what else happened? I didn’t understand

  • has to give the exit of the most repeated, but if 2 values repeat also has to give the exit of the highest value

Show 3 more comments

1 answer

0

Look if it helps you, it’s cm Js. Easy convert to the language you need.

var vetor = [5, 2, 5, 3, 2, 2, 5, 4, 3]; //vetor que precisa receber do usuário
var n = 9; // numero de casas do vetor
var nota = 0;
var aux = 0;
var cont = 1;
var contAtual = 1;
var i, j;

for (i = 0; i < n; i++) { //usando o método bolha para ordenação. Assim fica mais fácil executar as comparações dos valores.
    for (j = i + 1; j < n; j++) {
        if (vetor[i] < vetor[j]) { // estou ordenando pelo maior
            aux = vetor[i];
            vetor[i] = vetor[j];
            vetor[j] = aux;
        }
    }
}

for (i = 0; i < n; i++) {
    console.log(vetor[i]); //imprimir vetor ordenado
}

for (i = 1; i < n; i++) {
    if (vetor[i] == vetor[i - 1]) { //se o valor anterior for igual ao atual.
        cont++; //conta quantos repetiram
        aux = vetor[i - 1]; //salva o valor para verificar o maior
        if ((cont == contAtual && aux > nota) || (contAtual < cont)) { // esse if vc deve entender =>  se o cont atual for igual o anterior que já foi salvo e o valor na nota for maior. Salva o atual. OUUUUU, se o número de cont for maior.
            nota = aux;
            contAtual = cont;
        }
    }
    else { // reseta valores quando o próximo for diferente.
        cont = 1;
        aux = 0;
    }
}
console.log("A maior nota e ", nota);
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title>Hello, World!</title>
        <meta charset="utf-8">
    </head>
    <body>
        ...
        <script type="text/javascript" src="contarRepetidos.js"></script>
    </body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.