I am new to programming and trying to learn by exploring. I was looking for a solution to find sum of maximum time repeating integer in an array with best time complexity. Suppose we have [1, 2, 3, 3] the result should be 6 with least time complexity, say O(n).
I came up with a solution but not sure about the complexity. Need some help to understand if below mentioned code has least complexity or it could be better(definitely!). Sorry if I made any mistake and thanks in advance.
public static int mapBasedFinder(int[] arr){
Map<Integer, Integer> values = new HashMap<Integer, Integer>();
int result = 0;
for(int i=0; i<arr.length; i++){
int temp = arr[i];
if(values.containsKey(temp))
values.put(temp, values.get(temp)+1);
else
values.put(temp, 1);
Map.Entry<Integer, Integer> maxEntry = null;
for (Map.Entry<Integer, Integer> entry : values.entrySet())
{
int key = entry.getKey();
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
result = entry.getValue() * key;
}
}
}
return result;
}