Thursday 23 November 2017

C Program to print the element that is repeated maximum number of times in an array

If two elements are repeated same number of times, then the smallest element is printed

#include <stdio.h>
main()
{
    int n,i,c,high,max,a[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        c=0;
        for(j=i;j<n;j++)
        {
            if(a[i] == a[j])           
                c++;
        }
        if( i == 0)
        {
            max=c;
            high=i;
        }
        else
        {
            if( max<c)
            {
               
                max=c;
                high=i;

            }
            else if( max == c && a[high]> a[i])
                high=i;
        }
    }
printf("%d\t%d",a[high],max);
}

No comments:

Post a Comment