what is the best way to find most common number in a array in c++? like: arr[n] has 6, 4, 4, 4, 2, 1; now print 4 = 3; Please Help Me Thanks in Advance

asked 05 May '17, 12:42

smsaleque's gravatar image

smsaleque
1119


you can try this,

map < int, int > mp;
int number;

// input 10 5 5 5 10
for(int i = 0; i < 5; i++) {
    cin >> number;

    mp[number] += 1;
}
int max_count = 0;
map < int, int > :: iterator i;

for(i = mp.begin(); i != mp.end(); ++i) {
    if(i->second > max_count) {
        max_count = i->second;
    }
}
cout << max_count << endl; // output = 3
permanent link

answered 05 May '17, 13:34

menon's gravatar image

menon
4.7k437

menon bro thanks but i can't understand '->' this operator. please can you explain me about this '->' operator. (i->second) only this line explain please if you can. thanks in advance bro

(05 May '17, 14:30) smsaleque

map এ দুইটা বিষয় থাকে । ভালু, ইনডেক্স । ইনডেক্স অ্যাক্সেস করার জন্য i->first এবং ভালু অ্যাক্সেস করার জন্য i->second ব্যবহার করা হয় । map সম্পর্কে জানার জন্য এই লিঙ্ক দেখতে পারেন http://menonbrur.blogspot.com/2016/12/cpp-map.html

(05 May '17, 14:35) menon

very very thanks bro. now i understand the code. i saw in your link that map has many built-in function. can you tell me please some most important function for coding. i am asking you because it has many functions so i am confused. so please tell me some most important built-in function . thanks in advance

(05 May '17, 16:13) smsaleque

you can take a look at : http://www.cplusplus.com/reference/map/map/

If you get your answer then please mark the answer as ACCEPTED.

(05 May '17, 16:32) menon
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×56
×5
×1

question asked: 05 May '17, 12:42

question was seen: 693 times

last updated: 05 May '17, 16:32