import java.util.Hashtable; import java.util.Map; public class Frequences { public T mostFrequent(T[] tab) { Hashtable indexList = new Hashtable(); for (T element : tab) { indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1); } T element = tab[0]; Integer count = indexList.get(tab[0]); for(Map.Entry entry : indexList.entrySet()) { if (entry.getValue() > count) { element = entry.getKey(); count = entry.getValue(); } } return element; } public Liste getFrequences(T[] tab) { Hashtable indexList = new Hashtable(); for (T element : tab) { indexList.put(element, indexList.get(element) == null ? 1 : ((Integer)indexList.get(element)) + 1); } Association lastEntry = null; boolean first = true; Liste list = new Liste(); for(Map.Entry entry : indexList.entrySet()) { if (first) { lastEntry = new Association(entry.getKey(), entry.getValue()); list.setFirstElement(lastEntry); first = false; } else { Association newEntry = new Association(entry.getKey(), entry.getValue()); lastEntry.setSuivant(newEntry); lastEntry = newEntry; } } return list; } public static void main(String[] args) { Frequences f = new Frequences(); String[] tab = {"test", "test", "test", "12", "12", "12", "12"}; System.out.println(f.mostFrequent(tab)); } }