top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+2 votes
400 views
C: Given an array of numbers find n numbers with most occurrences?
posted Jun 12, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

I suppose you want to find the number , with most number of occurences:
please check below code

#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[0]);
 max_occurences(arr,n);
}
answer Oct 4, 2017 by Sushant Bose
Similar Questions
0 votes

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

0 votes

Given an unsorted array which has a number in the majority (a number appears more than 50% in the array), how to find that number?

+2 votes

Say you are given an array which has all duplicate members except one, which out this non-duplicate member.

...