write a program to calculate mode of list?

+1 vote
asked Dec 4, 2019 by teja

3 Answers

–2 votes
answered Dec 29, 2019 by somasekhar (140 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 Sam2676789 (150 points)
this is C, not python
0 votes
answered Jan 8 by Sam2676789 (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)))
0 votes
answered Feb 4 by Nando Abreu (930 points)

from statistics import mode

theList = [1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6]
print("\n{:19s}{:>3d} {}".format("The int list has:", len(theList), "elements..."))
print("{:19s}{:>3d} {}".format("Mode is:", mode(theList), "(mode = most frequent item)")

theList = ["Dog", "Cat", "Spider", "Snake", "Dog", "Dog", "Bear"]
print("\n{:19s}{:>3d} {}".format("Animal's list has:", len(theList), "elements..."))
print("{:16s}{:>6s} {}".format("Mode is:", mode(theList), "(mode = most frequent item)"))

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.
...