Without using include or include or pointers or include or i
Without using #include<map> or #include<cassert> or pointers or #include<stdio.h> or #include \"stdafx.h\"
Can only #include<iostream>, <iomanip>, <vector>, <algorithm>, and <string>
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it\'s similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: \"std::sort(result.begin(), result.end());\".
The most straightforward approach is to:
Iterate (loop) through the array to find out what the highest frequency for any value is without worrying about storing any modes.
Iterate through the array again, this time comparing the counts for each value to the highest frequency that you already found, if the count for a value is the same as the highest frequency, push that value into your results vector.
The file must be named: findMode.cpp
Solution
Here is the code of the given question and please do let me know if any errors occurs.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> findMode(int data[], int size)
{
int *uniqueArray = new int[size];
int *countArray = new int[size];
int item = data[0];
int frequency = 0;
int index = 0;
for (int i = 0; i< size; i++)
{
if (item == data[i])
{
frequency++;
}
else
{
uniqueArray[index] = data[i];
countArray[index] = frequency;
frequency = 0;
item = data[i];
}
}
int max = countArray[0];
int maxIndex = 0;
for (int j = 1; j< size; j++)
{
if (max < countArray[j])
{
max = countArray[j];
maxIndex = j;
}
}
vector<int> v1;
if (countArray[0] == 0 && maxIndex == 0)
{
for (int k = 0; k < size; k++)
{
v1.push_back(uniqueArray[k]);
}
}
else
{
v1.push_back(uniqueArray[maxIndex]);
}
for (int i = 0; i < (int)v1.size(); i++)
cout << v1.at(i) << endl;
return v1;
}
int main()
{
int data[] = { 2,1,1,1,1,1,1,1,1,1 };
int size = sizeof( data )/sizeof( int );
findMode( data, size );
return 0;
}


