/** * Calculate average of mode elements (most frequently appearing elements). * @param cont Container of numeric values. * @return Mode average. */ template<typename Cont> Cont::value_type ModeAverage(const Cont& cont) { typedef map<Cont::value_type, int> ValueCounter; ValueCounter counter;
// Count up value furequency and get max frequency. int maxFreq = 0; { Cont::const_iterator it; for (it = cont.begin(); it != cont.end(); it++) { int& c = counter[*it]; c += 1; maxFreq = MAX(maxFreq, c); } }
// Sum up container element of the max frequency. Cont::value_type sum = 0.0; int sumCount = 0; { ValueCounter::const_iterator it; for (it = counter.begin(); it != counter.end(); it++) { if ((*it).second == maxFreq) { sum += (*it).first; sumCount++; } } }
// Sum up container element of the max frequency. Cont::value_type sum = 0.0; int sumCount = 0; { ValueCounter::const_iterator it; for (it = counter.begin(); it != counter.end(); it++) { if ((*it).second == maxFreq) { sum += (*it).first; sumCount++; } } }