findung the mode in c 2b 2b

Solutions on MaxInterview for findung the mode in c 2b 2b by the best coders in the world

showing results for - "findung the mode in c 2b 2b"
Larry
20 Jan 2018
1int number = array[0];
2int mode = number;
3int count = 1;
4int countMode = 1;
5
6for (int i=1; i<size; i++)
7{
8      if (array[i] == number) 
9      { // count occurrences of the current number
10         ++count;
11      }
12      else
13      { // now this is a different number
14            if (count > countMode) 
15            {
16                  countMode = count; // mode is the biggest ocurrences
17                  mode = number;
18            }
19           count = 1; // reset count for the new number
20           number = array[i];
21  }
22}
23
24cout << "mode : " << mode << endl;