#How to calculate the mode of a set of data.

13 messages · Page 1 of 1 (latest)

wicked tartan
#

count how many occurrences there are for each f64 then get the most common one

#

but some impls for HashMap requires the key to impl Eq

#

and f64 doesnt impl Eq

#

because of NaN cases

#

if you can guarantee there is no NaN then you can unwrap the Ordering from PartialEq

#

so:

make a hashmap
go through `input`
  if the item is in the hashmap keys, increment the value
  otherwise create a pair with the key, and a value of 1
get the key of the biggest value in the hashmap
#

consider the return type of your function

#

what happens when input is empty?

latent quartz
#

The inputs are integers, so no f64s are needed for mode. Just count how many of each integer there are, and the mode will be a Vec<{integer}> of all of the values that have the maximum count.

#
  1. Given a list of integers, use a vector and return the median (when sorted, the value in the middle position) and mode (the value that occurs most often; a hash map will be helpful here) of the list.
#

So, for example, vec![1, 2, 2, 2, 3, 3, 4, 4, 4] has 2 and 4 as the mode.

latent quartz
#

Yes, you can make an associative array, like Vec<(f64, u64)>.

#

The f64 is for the value in the input, the u64 is for the count.