How do I determine the number that appears most often and the position it is in?

Asked

Viewed 1,991 times

2

It was performed the reading of a vector of n positions, as I do to determine the number that appears more and the position where it is?

programa {
    funcao inicio(){
        inteiro vetor[]
        inteiro numero, conta
        inteiro i, j, n

        leia(n)
        para (i = 0; i < n; i++){
            leia(vetor[i])
        }

        para (i = 0; i < n; i++){
            para (j = 0; j < n; j++){
                se (vetor[i] == vetor[j] e i != j){

                    escreva("numeros repetido ", vetor[i], " posição ", i,", ", j, "\n")
                }
            }
        }
    }
}

I can’t determine which number repeats the most, what I’ve been able to determine is where there are repeats.

  • And what’s the problem? What can we help you with?

  • What is this programming language? Since you are a new user, I would advise you to take a tour at this link.

  • https://answall.com/questions/208891/buscar-n%C3%Bameros-repetidos-em-lista-com-visualg

  • Good morning, try to create another vector and then write the position value of v1 and in v2 you use only counter. If you don’t succeed, let us know in the comments that I put the solution

2 answers

0

My resolution, I used two vectors of 5 positions in vetor1[5] and vetor2[5] because if not, it would not compile the reading of n will have to be mandatory 5, but it is the idea that can change on top

programa {

    funcao inicio(){
        // Declaração de variaveis
        inteiro vetor1[5], vetor2[5], n, chave, maior, j=1, i
        logico mensagem = verdadeiro

        // Leitura de n
        leia(n)

        // leitura do vetor principal
        para(i=1; i<n; i++){
            leia(vetor1[i])
        }

        // Vetor 2 define as repitições, deixando primeiro todas iguais a 0
        para(i=1; i<n; i++){
            vetor2[i] = 0
        }

        // Verifica se dois numeros são iguais e se sim aumenta no vetor repetições
        para(i=1; i<n; i++){
            para(j=1; j<n; j++){
                se (vetor1[i] == vetor1[j]){
                    vetor2[i] = vetor2[i] + 1
                }
            }
        }
        // Determina qual é o maior nº de repetições
        maior = 0
        para(i=1; i<n; i++){
            se(vetor2[i] > maior){
                maior = vetor2[i] 
            }
        }
        // Determina em que posições esta repetido e depois qual é o numero repetido
        para(i=1; i<n; i++){
            se (vetor2[i] == maior){
                chave = i
                // Utilizado o ciclo while para não repetir a mensagem o nº de vezes de repetições do vetor 2 
                enquanto(mensagem == verdadeiro){
                    escreva("\nNumero mais repetido: ", vetor1[chave], " e repete ", maior, " vezes")
                    mensagem = falso
                }
                escreva("\nPosições: ", i)
            }

        }       
    }
} 

-2

Here is the solution to the problem in Python 3:

def mais_repetido(array):
    s = set(array)
    d = {}

    for i in s:
        d[i] = 0

    for i in s:
        for j in array:
            if i == j:
                d[i] += 1

    maior = max(d.values())
    chave = 0

    for i in d:
        if d[i] == maior:
            chave = i
            break

    for i in range(len(array)):
        if array[i] == chave:
            print('Posição: {}'.format(i))
  • The ideal would not be that the answer is in portugol since the question is related to this?

  • You can take the algorithm and transcribe to Portugol :)

  • And does the user have both knowledge in Python how much in Portugol to do this?

  • 2

    Well, I tried to help, so long

  • 1

    @R.Santos in this case, answers in other languages should be welcome, because if someone finds the same problem with Python they will have the basis to solve. Also has the side that someone could translate to Portugol and by as response

Browser other questions tagged

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