-
Notifications
You must be signed in to change notification settings - Fork 2
/
most_frequent_num_arr.cpp
84 lines (67 loc) · 1.71 KB
/
most_frequent_num_arr.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/****************************************************************************
File name: most_frequent_num_arr.cpp
Author: babajr
*****************************************************************************/
#include <iostream>
#include <cassert>
using namespace std;
/* Approach 1: TC = O(n2) */
/*
int getMostFreqNum(int arr[], int size)
{
int max_repeat = -1;
int max_val = -1;
for(int i = 0; i < size; ++i)
{
int repeat = 0;
for(int j = 0; j < size; ++j)
{
if(arr[i] == arr[j])
repeat += 1;
}
if(max_repeat == -1 || max_repeat < repeat)
{
max_repeat = repeat;
max_val = arr[i];
}
}
if(max_repeat == 1)
return -1;
cout<<max_val<<" repeated "<<max_repeat<<" times \n";
return max_val;
}
*/
/*Approach 2: using another array. TC = O(n). */
int getMostFreqNum(int arr[], int size)
{
int freq[150 + 1] = {0}; //range of numbers 0 to 150 is given in the question
int max_pos = -1;
for(int i = 0; i < size; ++i)
freq[arr[i]]++;
for(int j = 0; j < 151; j++)
{
if(max_pos == -1 || (freq[max_pos] < freq[j]))
max_pos = j;
}
return max_pos;
}
void printArray(int arr[], int size)
{
for(int i = 0; i < size; ++i)
cout<<arr[i]<<" ";
cout<<"\n";
}
int main()
{
const int size = 5;
int most_freq_num;
int arr[size] = {1, 1, 3, 1, 5};
cout<<"Original Array: \n";
printArray(arr, size);
most_freq_num = getMostFreqNum(arr, size);
if(most_freq_num == -1)
cout<<"No repeating number in an array\n";
else
cout<<"Most frequent number in an array is: \t"<<most_freq_num;
return 0;
}