check on number of times a specific element appear...

Hi all,

i've got a problem in which i need some help in.

i've got a list on input as username in a form of a alphabet, any alphabet for each user.

now i need to check on the whole list to get any username that appears for more than 5 times.

i'm clueless on how to go about it...

any suggestion would be very much appreciated.

Thanks.
Where are you storing this list of usernames?
I am assuming you are storing this list in an array or a vector. Well there are several ways to do this, one of them requires that you understand the concept of map in c++.

http://www.cplusplus.com/reference/stl/map/

With the help of map you can do something like this:
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
#include<iostream>
#include<map>
using namespace std; 

int main() { 
char inpArr[] = {'a','b','c','a','c','b','d','a','a','a'};
map <char, int> myMap;
 
for (int i=0; i<sizeof(inpArr); i++) {
    if (myMap.count(inpArr[i]) == 0) {
          myMap.insert(pair<char, int>(inpArr[i],1));
        }
     else  {
            myMap[inpArr[i]]++;
     }
}
 
map <char, int>::iterator it;
for (it = myMap.begin(); it!=myMap.end(); it++) {
      if ((*it).second >= 5) {
         cout<<(*it).first<<endl;
      }

return 0;
}


Hope this helps

I am sorry in advance if we are not supposed to post STL code in beginners forum.

Topic archived. No new replies allowed.