Write a method from the client perspective to find the mode

Write a method from the client perspective to find the mode of a SortedListInterface object. The mode of a list of values is the value that has the greatest frequency (occurs the most). The method header is: public int getMode(SortedListInterface sList) For full credit, write an efficient solution (O(n )).

Solution

Answer:

#include <iostream>

#include <vector>

int getMode(const std::vector<int>& v)

{

unsigned max = 0, mod = -1;

for (int i = 1, cnt; i < v.size();++i)

{

if (v[i] == v[i - 1]) cnt += 1;

else if (max < cnt)

{ max = cnt; cnt = 0; mod = v[i - 1];

}

}

return mod;

}

int main(void)

{

std::vector<int> v{1, 1, 1, 2, 2, 3, 7, 7, 7, 7, 7, 9, 9};

std::cout << getMode(v)<< \"\ \";

}

Write a method from the client perspective to find the mode of a SortedListInterface object. The mode of a list of values is the value that has the greatest fre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site