   # C: Given an array of numbers find n numbers with most occurrences?

114 views
C: Given an array of numbers find n numbers with most occurrences? posted Jun 12, 2017

+1 vote

I suppose you want to find the number , with most number of occurences:

``````#include <stdio.h>
void max_occurences(int arr[],int n)
{
int count = 0;
int i=0,j=0;
int previous_count = 0;
int most_occured = 0;
for(int i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i] == arr[j])
{
count++;
}
}
if(previous_count < count)
{
previous_count = count;
most_occured = arr[i];
}
count = 0;
}
printf("Most occured number is %d\n",most_occured);
printf("Occured %d\n number of times",previous_count+1);
}
int main()
{

int arr[] = {0,1,1,2,1,2,2,2,2,2,1,1,1,1,1,1};
int k = sizeof(arr);
int n = sizeof(arr)/sizeof(arr);
max_occurences(arr,n);
}
`````` answer Oct 4, 2017
Similar Questions

Given an array of sorted integers and find the closest value to the given number. Array may contain duplicate values and negative numbers.

Example : Array : 2,5,6,7,8,8,9
Target number : 5
Output : 5

Target number : 11
Output : 9

Target Number : 4
Output : 5