//How to find majority element in array without using backtracking method.
#include<stdio.h>
main()
{
int *p,*q,n,i,j,c=0,f,fl=0;
printf("Enter the number of elments you want in your array list: ");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
q=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
q[i]=p[i];
}
for(i=0;i<=n/2;i++)
{
c=1;
if(p[i]==-1000)
continue;
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
c=c+1;
p[j]=-1000;
}
}
if(c>(n/2))
{
fl=1;
printf("%d is majority element",p[i]);
break;
}
}
if(fl==0)
printf("no majority element is found");
}
#include<stdio.h>
main()
{
int *p,*q,n,i,j,c=0,f,fl=0;
printf("Enter the number of elments you want in your array list: ");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
q=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
q[i]=p[i];
}
for(i=0;i<=n/2;i++)
{
c=1;
if(p[i]==-1000)
continue;
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
c=c+1;
p[j]=-1000;
}
}
if(c>(n/2))
{
fl=1;
printf("%d is majority element",p[i]);
break;
}
}
if(fl==0)
printf("no majority element is found");
}