-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumFrequencyNumber.java
More file actions
67 lines (62 loc) · 2.08 KB
/
MaximumFrequencyNumber.java
File metadata and controls
67 lines (62 loc) · 2.08 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Maximum Frequency Number
// Send Feedback
// You are given an array of integers that contain numbers in random order.
// Write a program to find and return the number which occurs the maximum times
// in the given input.
// If two or more elements are having the maximum frequency, return the element
// which occurs in the array first.
// Input Format:
// The first line of input contains an integer, that denotes the value of the
// size of the array. Let us denote it with the symbol N.
// The following line contains N space separated integers, that denote the value
// of the elements of the array.
// Output Format :
// The first and only line of output contains most frequent element in the given
// array.
// Constraints:
// 0 <= N <= 10^8
// Time Limit: 1 sec
// Sample Input 1 :
// 13
// 2 12 2 11 12 2 1 2 2 11 12 2 6
// Sample Output 1 :
// 2
// Sample Input 2 :
// 6
// 7 2 2 4 8 4
// Sample Output 2 :
// 2
// Explanation:
// Here, both element '2' and element '4' have same frequency but '2' ocurr
// first in orignal array that's why we are returning '2' as output.
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<>();
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i]) + 1);
} else {
map.put(arr[i], 1);
}
}
int maxOccuringNumber = 0;
int occurenceTimes = Integer.MIN_VALUE;
for (int i : arr) {
if (map.containsKey(i)) {
if (map.get(i) > occurenceTimes) {
maxOccuringNumber = i;
occurenceTimes = map.get(i);
}
}
}
return maxOccuringNumber;
}
}