Skip to content

Instantly share code, notes, and snippets.

@madoodia
Created April 29, 2016 08:53
Show Gist options
  • Save madoodia/dc44e662e18dec0565d44ddf03014acb to your computer and use it in GitHub Desktop.
Save madoodia/dc44e662e18dec0565d44ddf03014acb to your computer and use it in GitHub Desktop.
// Advanced Programming
// Instructor: Ramtin Khosravi
// 2015-2016
// madoodia@gmail.com
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int most_frequent(vector<int>);
int main()
{
vector<int> numbers = {2, 10, 6, 3, 4, 10, 2, 2, 10, 3};
int most = 0;
most = most_frequent(numbers);
printf("This number repeated more than others: %d", most);
return 0;
}
int most_frequent(vector<int> numbers){
vector<int> d_nums(numbers);
int counter = 0;
int repeat[int(d_nums.size())];
int max = 0;
int index = 0;
vector<int>::iterator it;
it = unique(d_nums.begin(), d_nums.end());
for(int i=0;i < d_nums.size();i++){
counter = 0;
for(int j=0;j < numbers.size();j++){
if(i == j){
counter++;
}
}
repeat[i] = counter;
}
for(int i=0;i < d_nums.size();i++){
if(i > max){
max = i;
index = i;
}
}
return d_nums[index];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment