33 lines
901 B
Java
33 lines
901 B
Java
|
import java.util.Hashtable;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class Frequence<T> {
|
||
|
public T mostFrequent(T[] tab) {
|
||
|
Hashtable<T, Integer> indexList = new Hashtable<T, Integer>();
|
||
|
|
||
|
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<T, Integer> entry : indexList.entrySet()) {
|
||
|
if (entry.getValue() > count) {
|
||
|
element = entry.getKey();
|
||
|
count = entry.getValue();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return element;
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
Frequence<String> f = new Frequence<String>();
|
||
|
|
||
|
String[] tab = {"test", "test", "test", "12", "12", "12", "12"};
|
||
|
|
||
|
System.out.println(f.mostFrequent(tab));
|
||
|
}
|
||
|
}
|