    #include <algorithm>
    #include <vector>
    #include <iterator>
    #include <set>
    #include <iostream>
    #include <numeric>
    
    using namespace std;
    
    int main()
    {
        std::vector<int> L = { 1, 2, 4, 3, 2, 4 };
        std::set<int> tempSet;
        //...
        // partition the elements, unique items on left, duplicates on right
        auto divider = stable_partition(L.begin(), L.end(), [&](int n)
        {
            // return true if item is not in the set, false otherwise
            return tempSet.insert(n).second;
        });
    
        // do something with the duplicates, for example, print them
        cout << "Here are the dups:\n";
        copy(divider, L.end(), ostream_iterator<int>(cout, " "));

        // get the average

        // get number of duplicates  
        size_t numDups = std::distance(divider, L.end());  
        double avg = 0.0;

        // compute average using std::accumulate
        if ( numDups > 0 ) 
           avg = std::accumulate(divider, L.end(), 0.0) / numDups;
        cout << "\nThe average of the duplicates is: " << avg << "\n";
    
        // erase the duplicates
        L.erase(divider, L.end());
    
        // print the updated vector now
        cout << "\n\nHere is the resulting vector:\n";
        copy(L.begin(), L.end(), ostream_iterator<int>(cout, " "));
    }
