-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maximum frequency number.cpp
57 lines (43 loc) · 1.31 KB
/
Maximum frequency number.cpp
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
/*
Maximum Frequency Number
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 contend for 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 :
3
1 4 5
Sample Output 2 :
1
*/
#include<unordered_map>
#include<utility>
int highestFrequency(int arr[], int n) {
// Write your code here
unordered_map<int, int> frequency;
for(int i = 0; i < n; i++) {
frequency[arr[i]]++;
}
int key = 0;
int value = 0;
for(int i = 0; i < n; i++) {
if(frequency[arr[i]] > value) {
key = arr[i];
value = frequency[arr[i]];
}
}
return key;
}
// Time Complexity : O(n)
// Auxillary Space : O(n)