0
Find the most occurred number and prints it
1-5 numbers only example 1 2 3 2 5 the most occurred number is 2
4 Answers
+ 6
@Kinshuk,
Works like a charm! yes the headers was apparently the missing part of it. Your answer must get best answer mark for this :)
(Edit)
And a good note on the (other) thread duplicate too :)
#highlysuggestedsolution
+ 5
@Kinshuk, is there a header required? I tried your code in Playground and get error message related to arr.begin & arr.end. Can you verify please..
+ 4
#include<iostream>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
int arr[5]; for(int &i:arr) cin>>i;
int maxe=0, maxctr=0;
for(int i : arr)
{
int c = count(begin(arr),end(arr),i);
if(c > maxctr)
{ maxe = i; maxctr = c; }
}
cout<<maxe<<" is the most frequent "
<<"element. \nIt occurred "<<maxctr
<<" times."<<endl;
}
+ 3
@Ipang
Sorry, arr.begin() isn't valid as arr is not a STL container. As for the code, just the header algorithm, iterator and iostream are required.