how to find the mode of a vector c 2b 2b

Solutions on MaxInterview for how to find the mode of a vector c 2b 2b by the best coders in the world

showing results for - "how to find the mode of a vector c 2b 2b"
Louanne
04 Oct 2020
1//copied from GeeksForGeeks
2#include <bits/stdc++.h> 
3using namespace std; 
4  
5// Function that sort input array a[] and 
6// calculate mode and median using counting 
7// sort. 
8void printMode(int a[], int n) 
9{  
10    int b[n]; 
11    int max = *max_element(a, a + n); 
12    int t = max + 1; 
13    int count[t]; 
14    for (int i = 0; i < t; i++) 
15        count[i] = 0; 
16    for (int i = 0; i < n; i++) 
17        count[a[i]]++; 
18    int mode = 0; 
19    int k = count[0]; 
20    for (int i = 1; i < t; i++) { 
21        if (count[i] > k) { 
22            k = count[i]; 
23            mode = i; 
24        } 
25    } 
26  
27    cout << "mode = " << mode; 
28} 
29//Just Include this in your template and call the function printMode 
30//to print the mode