I implemented a solution for: Finding the most frequent number from an array. How are my variable names? Or how could I improve this?
private static Integer findMostFrequentNumber(int[] givenArray) {
int mostFrequentNumber = 0;
if (givenArray != null) {
Map<Integer, Integer> numberOccuranceMap = new HashMap<>();
int mostFrequentOccurance = 0;
for (Integer number : givenArray) {
if (!numberOccuranceMap.containsKey(number)) {
numberOccuranceMap.put(number, 1);
} else {
numberOccuranceMap.put(number, numberOccuranceMap.get(number) + 1);
}
if (numberOccuranceMap.get(number) > mostFrequentOccurance) {
mostFrequentNumber = number;
mostFrequentOccurance = numberOccuranceMap.get(number);
}
}
}
return mostFrequentNumber;
}