Most occurring element in vector<int>

As the title say how do I get the most occurring element. I sorted the vector because I thought it would make things easier, but maybe that was unnecessary.

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
  int main()
{
    vector<int> randomIntegers;
    int num;
        
    default_random_engine generator(static_cast<int>(time(0)));
    uniform_int_distribution<int> random(1, 100);
        
    cout << " How many integers do you want to randomize (<=600): " ;
    cin >> num;
    
    for(int i=0; i < num; i++)
        randomIntegers.push_back(random(generator));
    
    int count = 1;
    for(auto e : randomIntegers)
    {
        cout << setw(5) << e;
        if (count++ % 15 == 0) 
            cout << "\n";
    }

    sort(randomIntegers.begin(),randomIntegers.end());
    
    cout << "\n" << "\n" << "Sorted vector" << "\n";
    count = 1;
    for(auto e : randomIntegers)
    {
        cout << setw(4) << e;
        if (count++ % 15 == 0) 
            cout << "\n";
    }
Last edited on
You could create a std::map, where keys are the unique values and the map-value is count of the key.

Then the move pairs in map into a vector. Sorting that vector by the count-column ...

However, consider this: {3,5,7,8,3,5,7}
produces:
{8,1}
{3,2}
{5,2}
{7,2}

And the "most occurring" is 3 and 5 and 7, for they all occur twice.
> I sorted the vector because I thought it would make things easier, but maybe that was unnecessary.

Assuming that we are interested in any one of the most frequently occurring elements:

By sorting the vector first, we can get the result by making one pass through the sorted vector.
O( N log N ) time (sort), O(1) space (no auxiliary data structure is required)

The other option is to use a secondary data structure - a hash table - to accumulate the frequencies.
O(N) time, O(M) space (where M is the number of unique values).

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
49
50
51
52
{
    // use auxiliary hash table: O(N) time, O(M) space ( M == number of unique elements )
    timer t ;

    std::unordered_map< int, int > counts ;
    for( int v : randomIntegers ) ++counts[v] ;

    const auto iter = std::max_element( std::begin(counts), std::end(counts),
            []( const auto& a, const auto& b ){ return a.second < b.second ; } ) ;

    std::cout << "\nunordered_map:\n(one) most occurring element: " << iter->first
              << " (occurred " << iter->second << " times)\n" ;
}

{
    // sort: O( N log N ) time, O(1) space
    timer t ;

    std::sort( std::begin(randomIntegers), std::end(randomIntegers) ) ;

    int curr_freq = 0 ;
    int max_freq = 0 ;
    int most_frequent_value = randomIntegers.front() ;
    int last_seen_value = randomIntegers.front() ;

    for( int value : randomIntegers )
    {
        if( value == last_seen_value ) ++curr_freq ;
        else
        {
            if( curr_freq > max_freq )
            {
                max_freq = curr_freq ;
                most_frequent_value = last_seen_value ;
            }

            last_seen_value = value ;
            curr_freq = 1 ;
        }
    }

    //////////////// edit: added //////////////////
    if( curr_freq > max_freq )
    {
        max_freq = curr_freq ;
        most_frequent_value = last_seen_value ;
    }
    ///////////////////////////////////////////////

    std::cout << "\nsort:\n(one) most occurring element: " << most_frequent_value
              << " (occurred " << max_freq << " times)\n" ;
}

http://coliru.stacked-crooked.com/a/b97c41adfefa4a34
http://rextester.com/DBUMD41597
Last edited on
Topic archived. No new replies allowed.