-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxFrequencyNumber.java
More file actions
39 lines (31 loc) · 866 Bytes
/
MaxFrequencyNumber.java
File metadata and controls
39 lines (31 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.HashMap;
public class Solution {
public static int maxFrequencyNumber(int[] arr){
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
HashMap<Integer,Integer> map=new HashMap();
int max=1;
int maxelem=arr[0];
for(int i=0;i<arr.length;i++)
{
if(map.containsKey(arr[i]))
{
int freq=map.get(arr[i]);
map.put(arr[i],freq+1);
}
else
map.put(arr[i],1);
int n=map.get(arr[i]);
if(n>max)
{
max=n;
maxelem=arr[i];
}
}
return maxelem;
}
}