C Code 25 _ To Print the top three elements with higher frequency(number of times a element is repeated) in the given array [Zoho Coding Round Question]


C Code 25 _ To Print the top three elements with higher frequency(number of times a element is repeated) in the given array [Zoho Coding Round Question]

Program:



#include<stdio.h>
int main()
{
                int a[50],b[50],c[50],d[50];
                int n,i,j,t,k=1,l=1,m=0,f=0;
                printf("Enter # of elements : ");
                scanf("%d",&n);
                printf("\n\nEnter %d elements : ",n);
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                                b[i]=a[i];
                }
                for(i=0;i<n-1;i++)
                {
                                for(j=i;j<n;j++)
                                {
                                                if(b[i]<b[j])
                                                {
                                                                t=b[j];
                                                                b[j]=b[i];
                                                                b[i]=t;
                                                }
                                }
                }
                printf("sorted array : ");
                for(i=0;i<n;i++)
                {
                                printf("%d ",b[i]);
                }
                c[0]=b[0];
                for(i=1;i<n;i++)
                {
                                if(b[i]==b[i-1])
                                {
                                                l=l+1;
                                }
                                if(b[i]!=b[i-1])
                                {
                                                c[k]=b[i];
                                                k=k+1;
                                                d[m]=l;
                                                m=m+1;
                                                l=1;
                                }
                }
                for(i=0;i<m-1;i++)
                {
                                for(j=i;j<m;j++)
                                {
                                                if(d[i]<d[j])
                                                {
                                                                t=d[j];
                                                                d[j]=d[i];
                                                                d[i]=t;
                                                                t=c[j];
                                                                c[j]=c[i];
                                                                c[i]=t;
                                                }
                                }
                }
                printf("\n\nThe top three highest frequency elements in the array : ");
                for(i=0;i<3;i++)
                {
                                printf("\n\n%d : %d times",c[i],d[i]);
                }
                return 0;
}



Output:








Thank You ...

Popular Posts