write a program to calculate mode of list?

0 votes
asked Dec 4, 2019 by teja

2 Answers

–1 vote
answered Dec 29, 2019 by (160 points)
#include<stdio.h>
int main()
{
    int n,a[10],i,j,count=0,maxcount=0,maxvalue=0;
    printf("size:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        count=0;
        for(j=1;j<n;j++)
        {
            if(a[j]==a[i])
            {
                count++;
            }
        }
        if(count>maxcount)
        {
            maxcount=count;
            maxvalue=a[i];
        }
    }
    if(maxcount!=count)
        printf("mode of numbers is:%d",maxvalue);
    else
        printf("there is no mode");
}
commented Jan 8 by (150 points)
this is C, not python
0 votes
answered Jan 8 by (150 points)
import collections
Numbers = [234, 423, 4, 67, 22, 22]

print(Numbers)

data = collections.Counter(Numbers)
data_list = dict(data)

print(data_list)

max_value = max(list(data.values()))
mode_val = [num for num, freq in data_list.items() if freq == max_value]
if len(mode_val) == len(Numbers):
   print("No mode found")
else:
   print("The Mode of the list is : " + ', '.join(map(str, mode_val)))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...