找出数组中元素个数最多的元素

找出数组中元素个数最多的元素。
例如数组:[1,2,5,3,2],元素2的个数最多,输出:2

#include<stdio.h>
int main()
{
    int a[10],b[10],t,m,k,i,j;
    int count = 0;
    while(scanf("%d", &a[count]) != EOF) 
    {
        count++;
    }
    for(i=0;i<count-1;i++)
    {
        for(j=i+1;j<count;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    t=a[0];
    m=1;
    k=1;
    for(i=1;i<count;i++)
    {
        if(a[i]!=a[i-1])
        {
            if(k>m)
            {
                m=k;
                t=a[i-1];
            }
            k=1;
        }
        else
        {
            k++;
        }
    }
    if(k>m)
    {
        m=k;
        t=a[i-1];
    }
    printf("%d\n",t);
}
原创文章 326 获赞 309 访问量 3万+

猜你喜欢

转载自blog.csdn.net/huangziguang/article/details/105986571