Search for int vector mode in C ++

So, I'm trying to create a basic program for learning the basics of C ++, I generate 100 random numbers from 0 to 100 and store them in a vector, then I show the sum, average, average, mode, high and low vector. I have everything else except the mode in which I am stuck. Here is the code I have.

int modeFunction() { numMode = 0; count = 0; for (int n = 0; n < 100; n++) { for (int y = 0; y < 100; y++) { if (numVector.at(y) == numVector.at(n)) { numMode = numVector.at(y); count++; } } } return numMode; } 

After that, I get stuck, because in my mind, which should work, but it is not. He simply puts the last number, usually 100. Any help would be greatly appreciated.

+6
source share
7 answers

since all values ​​are between 0 and 100, you can effectively find this mode using the histogram:

 std::vector<int> histogram(101,0); for( int i=0; i<100; ++i ) ++histogram[ numVector[i] ]; return std::max_element( histogram.begin(), histogram.end() ) - histogram.begin(); 
+7
source

Since mode is the number that is most common, you should not change numMode if the number of new numbers is greater than numMode count.

EDIT: To clarify, you need to keep a separate counter for the current item and the current number, which in your opinion is a mode. Ideally, setting newMode for the first element is a good approach.

In addition, the mode is not unique (ie, "1 1 2 2"). You may want to keep this in mind if you are interested.

 newMode = element[0] modeCount = # of occurrence of newMode for ( i-th element from [1 to end] ) { tmpCount = # of occurrence of element[i] if tmpCount > modeCount { newMode = element[i] modeCount = tmpCount } } 
+5
source

Your algorithm is incorrect - it outputs the last number in the array, because all this can ever do. Each time the number at index y matches the number at index n , you rewrite the results for the previous n . Since you use the same loop conditions, y and n always coincide at least at one point in the nested loop for every possible value of n , and you will always have numMode numVector.at(99) .

You need to change your algorithm in order to save the counter for each index n along the way (or at least the index n turned out to have the highest count ), so that you can find out at the end the cycle n , which was recorded most often.

+1
source

Alternative solutions. Note: unverified.

 int mode1(const std::vector<int>& values) { int old_mode = 0; int old_count = 0; for(size_t n=0; n < values.size(); ++n) { int mode = values[n]; int count = std::count(values.begin()+n+1, values.end(), mode); if(count > old_count) { old_mode = mode; old_count = count; } } return old_mode; } int mode2(const std::vector<int>& values) { return std::max_element(values.begin(), values.end(), [](int value) { return std::count(values.begin(), values.end(), value); }); } 
+1
source
An approach

bmcnett works great if the number of items is small enough. If you have a large number of elements, but all element values ​​are in a small range, using map / hashmap works well. Something like

 typedef std::pair<int, int> mode_pair; struct mode_predicate { bool operator()(mode_pair const& lhs, mode_pair const& rhs) { return lhs.second < rhs.second; } }; int modeFunction() { std::map<int, int> mode_map; for (int n = 0; n < 100; n++) mode_map[numVector[n]]++; mode_predicate mp; return std::max_element(mode_map.begin(), mode_map.end(), mp)->first; } 
+1
source

Mode means the number with the highest frequency. Logic should be -

 //Start of function int mode = 0, globalCount = 0 ; // Start of outer for loop for i = 0 to length - 1 int localCount = 0 // Start of inner for loop for j = 0 to length - 1 if vec[i] == vec[j] ++localCount // End of Inner for loop if ( localCount > globalCount ) globalCount = localCount mode = vec[i] // End of outer for loop if globalCount > 1 // This should be checked whether vec has repetitions at all return mode else return 0 // End of function 
0
source
  int number = array_list[0]; int mode = number; int count = 1; int countMode = 1; for (int i=1; i<size_of_list; i++) { if (array_list[i] == number) { // count occurrences of the current number count++; if (count > countMode) { countMode = count; // mode is the biggest ocurrences mode = number; } } else { // now this is a different number if (count > countMode) { countMode = count; // mode is the biggest ocurrences mode = number; } count = 1; // reset count for the new number number = array_list[i]; } } 
-one
source

All Articles