Find the element with the highest number

Asked

Viewed 606 times

3

I’m doing a basic election system, but I have a problem telling which candidate was elected. In this case, I added three candidates (x, y, z) to a ArrayList, I put two votes on x, three on y and four on z. When I want to know which one won (in this case which got the most votes) he prints all 3 candidates.

public void eleitoPresidente() {
    int maior = 0;
    for (Candidato e: candidato) {
        if(e.votos > maior) {
            maior = e.votos;
            System.out.println("O presidente eleito é: " + e.nome);
        }
    }
}

I want him to print only the candidate with the largest number of votes.

  • removes the println from the for and displays the Candidate outside of it.

3 answers

4


Keeping the algorithm you did initially has two problems, it is not keeping which is the biggest anywhere. And it is saying who was elected every time a new candidate evaluated has more votes than the previous one. So the correct one would be:

import java.util.*;

class HelloWorld {
    public static void main (String[] args) {
        ArrayList<Candidato> candidato = new ArrayList<>();
        candidato.add(new Candidato("joao", 4));
        candidato.add(new Candidato("jose", 2));
        candidato.add(new Candidato("joaquim", 7));
        int maiorVotos = 0;
        Candidato maiorCandidato = null;
        for (Candidato e: candidato) {
            if(e.votos > maiorVotos) {
                maiorVotos = e.votos;
                maiorCandidato = e;
            }
        }
        if (maiorCandidato != null) {
            System.out.println("O presidente eleito é: " + maiorCandidato.nome);
        }
    }
}

class Candidato {
    public String nome;
    public int votos;
    public Candidato (String nome, int votos) {
        this.nome = nome;
        this.votos = votos;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

You can use the method Collections.sort(List<T>, Comparator<? super T>) to order candidates by some criterion (in this case the number of votes in descending order), and then take the first of them:

public void eleitoPresidente() {
    List<Candidato> copia = new ArrayList<>(candidato);
    Collections.sort(copia, (a, b) -> b.votos - a.votos);
    System.out.println("O presidente eleito é: " + copia.get(0).nome);
}
  • 1

    I thought about doing it this way, but I gave it up 'cause I already screwed up for giving a simpler solution :)

1

That way it will work

public void eleitoPresidente() {
    Candidato maiorCand = null;
    int maior = 0;
    for (Candidato e : candidato) {
        if (e.votos > maior) {
            maiorCand = e;
        }
    }
    if (maiorCand != null) {
        System.out.println("O presidente eleito é: " + maiorCand.nome);
    } else {
        System.out.println("Não teve presidente eleito");
    }
}
  • 1

    Will give a build error because maiorCand may not have been initialized at the System.out. To solve this, just assign null in the statement of maiorCand and let give NullPointerException if there are no candidates.

  • 1

    Fixed issue

Browser other questions tagged

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