#How to calculate the mode of a set of data.
13 messages · Page 1 of 1 (latest)
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?
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.
- 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.