//How to find number of occurence of an element in Array and also find majority element.
#include<stdio.h>
main()
{
int *p,*q,*r,n,i,j,l=0,f;
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));
r=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
{
f=0;
for(j=0;j<i;j++)
{
if(p[i]==p[j])
{
f=1;
break;
}
}
if(f==1)
continue;
q[l]=1;
r[l]=p[i];
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
q[l]=q[l]+1;
}
}
l=l+1;
}
q=(int *)realloc(q,l*sizeof(int));
r=(int *)realloc(r,l*sizeof(int));
printf("\n");
for(i=0;i<l;i++)
printf("%d occurs %d times\n",r[i],q[i]);
for(i=0;i<l;i++)
{
if(q[i]>(float)(n/2))
printf("%d item is majority elemnt",r[i]);
}
getch();
}
#include<stdio.h>
main()
{
int *p,*q,*r,n,i,j,l=0,f;
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));
r=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
{
f=0;
for(j=0;j<i;j++)
{
if(p[i]==p[j])
{
f=1;
break;
}
}
if(f==1)
continue;
q[l]=1;
r[l]=p[i];
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
q[l]=q[l]+1;
}
}
l=l+1;
}
q=(int *)realloc(q,l*sizeof(int));
r=(int *)realloc(r,l*sizeof(int));
printf("\n");
for(i=0;i<l;i++)
printf("%d occurs %d times\n",r[i],q[i]);
for(i=0;i<l;i++)
{
if(q[i]>(float)(n/2))
printf("%d item is majority elemnt",r[i]);
}
getch();
}