# How to find Mode of a list [closed]

I want to find the mode (i.e. most frequent element) of a list of values. I saw some example with distributions but I'm working with lists that contain numbers. Suppose that I have a lits like:

data=RandomVariate[BetaDistribution[6, 17], 1000]


I realized that one way is using SmoothKernelDistribution of the given list and finding its maximum by using this code:

ArgMax[PDF[SmoothKernelDistribution[data],x],x]


The point is that the above code returns a value which may not be in observed data list.

Any idea on finding mode of the above list?

-

## closed as off-topic by Mr.Wizard♦Jul 15 '13 at 11:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

Have You read this, have You tried to search anything in documentation? –  Kuba Jul 15 '13 at 5:58
Commonest[data] –  rm -rf Jul 15 '13 at 6:06
@rm-rf I'm not sure if it is good approach for continuous distribution. –  Kuba Jul 15 '13 at 6:09
@Kuba He has a list of values, of which he wants the mode, and that is given by Commonest. Of course, whether a mode is a meaningful metric here is a different issue, which is left to the OP. –  rm -rf Jul 15 '13 at 6:16
@rm-rf I've tried Cmmonest but it returned a list of values. I want to have one value. –  Amin Jul 15 '13 at 15:00

Surely Commonest is the fastest and easiest way. But say you didn't know about Commonest, you could do it yourself... here's a list of values.

list = {1, 2, 3, 3, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9};


The mode is the value that occurs the most times. So we can use Tally to find out how many times each occurs:

Tally[list]

{{1, 1}, {2, 2}, {3, 4}, {4, 2}, {5, 1}, {6, 1}, {7, 1}, {8, 1}, {9, 1}}


which shows that 2 occurs twice, 3 occurs 4 times etc. To find the one that occurs the most, sort the list by the second element and then pick the last one:

Last[Sort[Tally[list], #1[[2]] < #2[[2]] &]]

{3,4}


So now you see the 3 is the most common element. Gathering all these together:

 First[Last[Sort[Tally[list], #1[[2]] < #2[[2]] &]]]

3


For the case of a continuous distribution, as shown by a histogram, one can use

histData = HistogramList[data]


which provides a list of the binned values and their frequency. The bin in which the largest number occur is a good candidate. This can be found by finding the index of the maximum element and then locating the edges of that histogram bin.

indMax = Ordering[histData[[2]]][[-1]];
histData[[1]][[indMax ;; indMax + 1]]


or the distribution above, this gives the range {13/50, 7/25}, which is the histogram bin that has the largest number of entries.

-
Notice that this method is not very useful for his sample. –  Kuba Jul 15 '13 at 10:00
@bill s , thank you for your response.I tried your code but it returned a value which its Count was 1. –  Amin Jul 15 '13 at 15:07
Amin -- this is no doubt because each value only occurs once in a continuous distribution. See if the HistogramList method helps. –  bill s Jul 15 '13 at 21:21
@bills, the idea of HistogramList it's what I'm looking for as the mean of the interval of bin containing largest number of entries returns the estimate of my interest.Thanks. –  Amin Jul 18 '13 at 2:07