Vectors in Rust are a collection type that can store multiple values of the same type. They are similar to arrays in JavaScript and lists in Python but with some additional features, they are stored on the heap and they can grow or shrink in size.

In this challenge you're asked to implement two functions that calculate the median and mode of a list of integers.

Read the definitions of each Median and Mode below to understand what you need to implement.

The median is the middle value of a list when it is ordered by size. If the list has an even number of elements, the median is the average of the two middle numbers.

Here's an example:

Or if the number of elements is even:

You can implement the function in the following steps:

Sort the list of numbers.

If the number of elements is odd, return the middle element.

If the number of elements is even, return the average of the two middle elements.

The mode is the number that appears most frequently in a list, if there are multiple numbers that appear the same number of times, return all of them in a vector.

Here's an example:

The number 1 appears four times, which is more than any other number in the list.

Important: You must sort the list of numbers before returning the modes list.

You can use the entry method to insert a value into a hash map if it doesn't exist or update it if it does.

When looping through a vector, you can dereference the value using & to get the value instead of a reference to it. For example:

Store the maximum frequency and the number that appears most frequently in a HashMap<i32, i32> and update it as you iterate through the list of numbers, and finally return the list of numbers that appears most frequently.

Editor not available on Mobile!

In order to solve the challenge, please visit this page on a desktop computer.

Solutions

n

nodbew

19 days ago

use std::collections::HashMap;

pub fn median(numbers: &mut Vec<i32>) -> f32 {

// TODO: Implement logic here to return the median of the list

numbers.sort();

if numbers.len() % 2 == 0 {

let idx = numbers.len() / 2;

((numbers[idx] + numbers[idx - 1]) as f32) / 2.0

} else {

numbers[(numbers.len() + 1) / 2 - 1] as f32

}

}

pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {

if numbers.len() == 0 {

return vec![];

}

use std::collections::HashMap;

let mut counts: HashMap<i32, u32> = HashMap::new();

for x in numbers.iter() {

*counts

.entry(*x)

.or_insert(0)

+= 1

}

let max = counts.values().max().unwrap().clone();

counts

.into_iter()

.filter(|(_, x)|*x == max)

.map(|(k, _)|k)

.collect()

}

h

herlock77

20 days ago

use std::collections::HashMap;

pub fn median(numbers: &mut Vec<i32>) -> f32 {

// TODO: Implement logic here to return the median of the list

numbers.sort();

let length = numbers.len();

match length % 2 == 0 {

true => {

(numbers[length/2] + numbers[length/2 - 1]) as f32 / 2.0

},

false => numbers[length/2] as f32,

}

}

pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {

// TODO: Implement logic here to return the mode of the list