Votes.compareTo(Votes[i])==Votes.compareTo(Votes[j])
public static int occurence2(String Votes[]){ for(int i=0;i<Votes.length;i++){ int nombreOccurence=0; for(int j=0;j<Votes.length;j++){ if(Votes[i].compareTo(Votes[j])==0){ nombreOccurence++; } } return nombreOccurence; } }
if(Votes[i].equals(Votes[j])){
public static int occurence2(String Votes[]){ for(int i=0;i<Votes.length;i++){ int nombreOccurence=0; for(int j=0;j<Votes.length;j++){ if(Votes[i].equals(Votes[j])){ nombreOccurence++; } } } return nombreOccurence; }
error: cannot find symbol
return nombreOccurence;
^
symbol: variable nombreOccurence
import java.util.HashMap; import java.util.Map; public class Test { public static <E> Map<E, Integer> occurence(E[] array) { Map<E, Integer> map = new HashMap<E, Integer>(); if (array == null) return map; for (E elt : array) { Integer cpt = map.get(elt); map.put(elt, cpt == null ? 1 : cpt + 1); } return map; } public static void main(String args[]) { String[] votes = { "x", "z", "y", "y", "z", "x", "x" }; System.out.println(occurence(votes)); // {x=3, y=2, z=2} } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question/** * @return le nombre de fois que str est dans tab */ public static int occurences(String str, String[] tab) { // ... } /** * @return true si le nombre d'occurence de str dans tab est supérieur à la moitié de la taille de tab. */ public static boolean majoritaire(String str, String[] tab) { // ... }
public static <E> boolean majoritaire(E[] array) { for (Integer cpt : occurence(array).values()) if (cpt > array.length / 2) return true; return false; }