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?

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?

0 votes

Hi, I think you want to find the number which present maximum times in an unsorted array:

```
#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);
}
```

...