Active8 months ago
Finding the Mode of Integers in an. I have recently analyzed four ways to calculate mode of the. Analasys program max and min and mode and graphing histogram. I have to calculate the mean, median, and mode for a lot a numbers. Does anyone have a quick Java algoritm to calculate the median, and mode of a lot of unsorted. I will be reading in the values as the program runs, in the form of doubles.
Are there any functions (as part of a math library) which will calculate mean, median, mode and range from a set of numbers.
user339108user3391085,1902828 gold badges7070 silver badges108108 bronze badges
6 Answers
Yes, there does seem to be 3rd libraries (none in Java Math). Two that have come up are:
but, it is actually not that difficult to write your own methods to calculate mean, median, mode and range.
MEAN
MEDIAN
MODE
UPDATE
As has been pointed out by Neelesh Salpe, the above does not cater for multi-modal collections. We can fix this quite easily:
ADDITION
If you are using Java 8 or higher, you can also determine the modes like this:
Nico HuysamenNico Huysamen7,63499 gold badges5050 silver badges7878 bronze badges
Check out commons math from apache. There is quite a lot there.
![Calculate Calculate](/uploads/1/2/6/5/126582585/378158299.png)
29.1k99 gold badges5252 silver badges9999 bronze badges
Calculate Range
javamonkey79javamonkey7911.3k3434 gold badges9696 silver badges158158 bronze badges
Test data
Modes {1,3} for { 3, 1, 5, 2, 4, 1, 3, 4, 3, 2, 1, 3, 4, 1 };
Modes {-1} for { 3, 1, 5, 2, 4, 1, 3, 4, 3, 2, 1, 3, 4, 1 ,-1,-1,-1,-1,-1}; Serial numbers and activation keys.
AthafoudModes {-1} for { 3, 1, 5, 2, 4, 1, 3, 4, 3, 2, 1, 3, 4, 1 ,-1,-1,-1,-1,-1}; Serial numbers and activation keys.
2,12122 gold badges3030 silver badges4646 bronze badges
Neel SalpeNeel Salpe57122 gold badges1010 silver badges2525 bronze badges
WIllWIll
If you only care about unimodal distributions, consider sth. like this.
stuchl4n3kstuchl4n3k
As already pointed out by Nico Huysamen, finding multiple mode in Java 1.8 can be done alternatively as below.
Happy coding!
Hashashihn AltheimHashashihn Altheim
protected by Community♦Sep 13 '16 at 12:31
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?