In: Computer Science
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values being between 0 and 100. java
import java.util.HashMap;
public class mode {
public static int mode(int[] array) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
int max = 1;
int temp = 0;
for (int i = 0; i < array.length; i++) {
if (hm.get(array[i]) != null) {
int count = hm.get(array[i]);
count++;
hm.put(array[i], count);
if (count > max) {
max = count;
temp = array[i];
}
} else hm.put(array[i], 1);
}
return temp;
}
public static void main(String[] args) {
int[] n = new int[] { 27, 15, 15, 11, 27 };
System.out.println(mode(n));
}
}
******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION
******************************************************************************************