-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_30.cpp
48 lines (43 loc) · 901 Bytes
/
question_30.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
// Write a C++ program to find the most frequent element in an array of integers
#include<iostream>
using namespace std;
int mostFrequent(int arr[], int n)
{
cout << endl;
int count = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] == arr[i+1])
{
cout <<"The most frequent element is : " << arr[i] << " ";
}
}
}
int main()
{
int n;
cout << "Enter size of an array : " << " ";
cin >> n;
int arr[100];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
mostFrequent(arr , n);
}