top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Given an unsorted array which has a number in the majority, how to find that number?

0 votes
657 views

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?

posted Jun 1, 2017 by Pooja Bhanout

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

1 Answer

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

...