c program to find majority element in an unsorted array

C program to find majority element in an unsorted array

below is the program to find majority elements in an unsorted Array

#include < stdio.h >
#define ARRAY_SIZE 100
void getMajorityElement(int *array, int size)
{
int i, majorityIndex = 0, count = 1;
/* Find Majority Element */
for(i = 1; i < size; i++)
{
/* Check if current element is same as majority element, If yes then increment count otherwise decrement count */
if(array[majorityIndex] == array[i])
count++;
else
count--;
if(count == 0)
{
majorityIndex = i;
count = 1;
}
}
/* Verify, If array[majorityIndex] is the majority element */
count = 0;
/* Count the frequency of array[majorityIndex] in array */
for (i = 0; i < size; i++)
{
if(array[i] == array[majorityIndex])
count++;
}
/* Check if count of majority element is more than size/2, If yes, then it is a majority element otherwise not */
if(count > (size/2))
printf("Majority Element : %d\n", array[majorityIndex]);
else
printf("No Majority Element Found\n");
}
int main()
{
int i, array[ARRAY_SIZE], count, sum;
printf("Enter the number of elements in Array\n");
scanf("%d", &count);
printf("Enter %d numbers\n", count);
for(i = 0; i < count; i++)
{
scanf("%d", &array[i]);
}
getMajorityElement(array, count);
return 0;
}

Output

Enter the number of elements in Array
7
Enter 7 numbers
4 2 8 7 3 1 3 3 3
Majority Element : 3
Enter the number of elements in Array
7
Enter 7 numbers
4 2 8 7 6 1 9 5 3
No Majority Element Found