I'm supposed to "Write a method that prints out the top 5 most commonly occurring numbers in an array along with the count of occurrences for each" I have a method that prints out the occurrence of each entry in an array and I created another method to print the top 5 occurrences in the array. I'm able to get the values to display correctly in descending order but I'm having trouble getting the keys to do the same. For Example, my array is [7, 8, 5, 3, 9, 3, 9, 4, 7, 3, 6, 9, 3, 2, 9, 6, 5, 7, 6, 3]. For the top5 method my my output is "2 occurs: 5 times 3 occurs: 4 times 4 occurs: 3 times 5 occurs: 3 times 6 occurs: 2 times" when it should be "3 occurs: 5 times 9 occurs: 4 times 6 occurs: 3 times 7 occurs: 3 times 5 occurs: 2 times" How can I fix my method to get the expected output?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
public class HW4 {
public static void main(String[] args) {
int[] a = getRandom(20, 1, 10);
System.out.println(Arrays.toString(a));
System.out.println();
occurrence(a);
System.out.println();
top5(a);
}
public static int[] getRandom(int n, int low, int high) {
long seed = 0;
Random random = new Random(seed);
random.setSeed(seed);
int[] result = new int[n];
for (int i = 0; i < n; i++)
result[i] = random.nextInt(high - low) + low;
return result;
}
public static void occurrence(int[] x) {
HashMap<Integer, Integer> occurrences = new HashMap<>();
for (int key : x) {
if (occurrences.containsKey(key)) {
occurrences.put(key, occurrences.get(key) + 1);
} else {
occurrences.put(key, 1);
}
}
for (int key : occurrences.keySet()) {
System.out.println(key + " occurs: " + occurrences.get(key) + " times");
}
}
public static void top5(int[] arr) {
HashMap<Integer, Integer> lookup = new HashMap<Integer, Integer>();
for (int key : arr) {
if (lookup.containsKey(key)) {
lookup.put(key, lookup.get(key) + 1);
} else {
lookup.put(key, 1);
}
}
ArrayList<Integer> keys = new ArrayList<>(lookup.keySet());
ArrayList<Integer> values = new ArrayList<>(lookup.values());
for (int i = 0; i < 5; i++) {
Collections.sort(values, Collections.reverseOrder());
System.out.println(keys.get(i) + " occurs: " + values.get(i) + " times");
}
}
}