I have to find the element with highest occurrences in a double array. I did it like this:
int max = 0;
for (int i = 0; i < array.length; i++) {
in
Use Collections.frequency
option:
List list = Arrays.asList("1", "1","1","1","1","1","5","5","12","12","12","12","12","12","12","12","12","12","8");
int max = 0;
int curr = 0;
String currKey = null;
Set unique = new HashSet(list);
for (String key : unique) {
curr = Collections.frequency(list, key);
if(max < curr){
max = curr;
currKey = key;
}
}
System.out.println("The number " + currKey + " happens " + max + " times");
Output:
The number 12 happens 10 times