How to calculate median of a Map<Int,Int>?

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-06-16T11:47:17Z Indexed on 2010/06/16 11:52 UTC
Read the original article Hit count: 195

Filed under:
|
|

For a map where the key represents a number of a sequence and the value the count how often this number appeared in the squence, how would an implementation of an algorithm in java look like to calculate the median?

For example:

1,1,2,2,2,2,3,3,3,4,5,6,6,6,7,7

in a map:

Map<Int,Int> map = ...
map.put(1,2)
map.put(2,4)
map.put(3,3)
map.put(4,1)
map.put(5,1)
map.put(6,3)
map.put(7,2)

double median = calculateMedian(map);
print(median);

would result in:

> print(median);
3
>

So what i am looking for is a java implementation of calculateMedian.

© Stack Overflow or respective owner

Related posts about java

Related posts about algorithm