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

5th Dec 2017, 3:22 AM
GODOF7
GODOF7 - avatar
4 Antworten
+ 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
5th Dec 2017, 12:39 PM
Ipang
+ 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..
5th Dec 2017, 11:50 AM
Ipang
+ 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; }
5th Dec 2017, 4:32 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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.
5th Dec 2017, 12:14 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar