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:
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.
sort
method to sort a vector.entry
method to insert a value into a hash map if it doesn't exist or update it if it does.&
to get the value instead of a reference to it. For example: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.yoakemae
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 len = numbers.len(); let index = len / 2; println!("{:?}",numbers); if len % 2 == 0 { println!("{:?}",len); return ((numbers[index] + numbers[index - 1]) as f32 / 2.0 as f32) as f32 } else { return numbers[index] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.is_empty() { return Vec::new(); } let mut counts = HashMap::new(); for &num in numbers { *counts.entry(num).or_insert(0) += 1; } let mut max_count = 0; let mut modes = Vec::new(); for (&num, &count) in counts.iter() { if count > max_count { max_count = count; modes.clear(); modes.push(num); } else if count == max_count { modes.push(num); } } modes.sort(); modes}
DivineGod
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 n = numbers.len(); let half = (n as f32)/2.0; match n { n if n%2 == 0 => { ((numbers[(half - 1.0) as usize] + numbers[half as usize]) as f32) / 2.0 } _ => numbers[half as usize] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut map = HashMap::<i32, u32>::with_capacity(numbers.len()); let mut max_count = 0u32; for n in numbers { map.entry(*n).and_modify(|entry| { let count = *entry + 1; *entry = count; max_count = std::cmp::max(max_count, count); }).or_insert(1); } let mut result = map.iter().filter(|(_,v)| **v == max_count).map(|(k,_)| *k).collect::<Vec<_>>(); result.sort(); result}
sroas
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 middle = numbers.len() / 2; if numbers.len() % 2 == 0 { (*numbers.get(middle - 1).unwrap() + *numbers.get(middle).unwrap()) as f32 / 2.0 } else { *numbers.get(middle).unwrap() as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let map = numbers.iter().fold(HashMap::new(), |mut m, cur| { m.entry(cur).and_modify(|cnt| *cnt += 1).or_insert(1); m }); let max = map .iter() .fold(0, |max, (_, v)| if *v > max { *v } else { max }); map.into_iter() .filter_map(|(k, v)| if v == max { Some(*k) } else { None }) .collect::<Vec<_>>()}
nt2311-vn
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); if len % 2 == 0 { let first_mid = len/2; let second_mid = len/2 -1; (numbers[first_mid] + numbers[second_mid]) as f32/ 2.0 } else { let mid = len/2; numbers[mid] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map: HashMap<i32, i32> = HashMap::new(); // Count occurrences of each number for &number in numbers { _ = *map.entry(number).and_modify(|val| *val += 1).or_insert(1); } // Find the maximum frequency let max_frequency = map.values().copied().max().unwrap_or(0); // Collect all keys with the maximum frequency map.into_iter() .filter(|&(_, count)| count == max_frequency) .map(|(key, _)| key) .collect()}
qiyuan711
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let size = numbers.len(); numbers.sort(); if size % 2 == 0 { (numbers[size / 2 - 1] + numbers[size / 2]) as f32 / 2.0 } else { numbers[size / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut frequencys = HashMap::new(); for num in numbers { *frequencys.entry(*num).or_insert(0) += 1; } let m = frequencys.values().cloned().max().unwrap_or(0); frequencys.iter().filter(|&(_, &value)| value == m).map(|(&key, _)| key).collect()}
tinthid
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { if numbers.is_empty() { panic!("Cannot calculate median of empty vector"); } // Sort the vector numbers.sort(); let len = numbers.len(); // If length is odd, return middle element // If length is even, return average of two middle elements if len % 2 == 0 { let mid = len / 2; (numbers[mid - 1] + numbers[mid]) as f32 / 2.0 } else { numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.is_empty() { return Vec::new(); } // Create a HashMap to store frequency of each number let mut frequency_map: HashMap<i32, i32> = HashMap::new(); // Count frequency of each number for &num in numbers { *frequency_map.entry(num).or_insert(0) += 1; } // Find the maximum frequency let max_frequency = frequency_map.values().max().unwrap(); // Collect all numbers that appear with maximum frequency let mut modes: Vec<i32> = frequency_map .iter() .filter(|&(_, &count)| count == *max_frequency) .map(|(&num, _)| num) .collect(); // Sort the modes for consistent output modes.sort(); modes}
nknknknkn2
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let len = numbers.len(); return match numbers.get(len / 2) { Some(v) => { if len % 2 == 0 { let v1 = *numbers.get(len / 2).unwrap() as f32; let v2 = *numbers.get(len / 2 + 1).unwrap() as f32; return (v1 + v2) / 2.0; } else { return *v as f32; } }, None => 0.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.len() == 0 { return numbers.to_vec(); } // TODO: Implement logic here to return the mode of the list let mut count_map = HashMap::new(); for &num in numbers { *count_map.entry(num).or_insert(0) += 1; } let max_count = count_map.values().cloned().max().unwrap_or(1); let mut result: Vec<i32> = count_map.into_iter() .filter(|&(_, count)| count == max_count) .map(|(num, _)| num) .collect(); result.sort(); return result;}
r1kk1s
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() % 2 == 0 { let n = numbers.len() / 2; (numbers[n] + numbers[n - 1]) as f32 / 2.0 } else { numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map = HashMap::new(); for &number in numbers { let count = map.entry(number).or_insert(0); *count += 1; } let max_value = map.values().max().unwrap(); let mut r = Vec::new(); for (key, value) in map.iter() { if value == max_value { r.push(*key); } } r.sort(); r}
Thymelizabeth
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 n = numbers.len() / 2; (numbers[n] + numbers[n - 1]) as f32 / 2. } else { numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let freq = numbers.into_iter().fold(HashMap::new(), |mut acc, n| { acc.entry(n).and_modify(|f| *f += 1).or_insert(1); acc }); let max_freq = *freq.values().max().unwrap_or(&0); let mut result = freq.into_iter().filter_map(|(k, v)| if v == max_freq { Some(*k) } else { None }).collect::<Vec<_>>(); result.sort(); result}
tamanishi
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(); println!("numbers : {:?}", numbers); if numbers.len() == 1 { numbers[0] as f32 } else if numbers.len() % 2 == 0 { (numbers[numbers.len() / 2 - 1] as f32 + numbers[numbers.len() / 2] as f32) / 2 as f32 } else { numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut map = HashMap::new(); for &number in numbers { let count = map.entry(number).or_insert(0); *count += 1; } let m = map.values().max().unwrap(); let mut r = Vec::new(); for (key, value) in map.iter() { if value == m { r.push(*key); } } r}
pynhpo
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let i = (numbers.len() / 2) as usize; if numbers.len() % 2 != 0 { return numbers[i] as f32 } else { return (numbers[i] as f32 + numbers[i - 1] as f32) / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map: HashMap<i32, i32> = HashMap::new(); for &number in numbers { *map.entry(number).or_insert(1) += 1; } let values: Vec<&i32> = map.values().collect(); let max_value = values.iter().max(); if let Some(&&max) = max_value { let mut keys: Vec<i32> = map.iter() .filter(|&(_, &v)| v == max) .map(|(&k, _)| k) .collect(); keys.sort(); return keys } else { return vec![0] }}
konishu
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { // ソートして中央値を計算 numbers.sort_unstable(); // メモリ効率の良いソート関数を使用 let len = numbers.len(); if len % 2 == 0 { (numbers[len / 2 - 1] + numbers[len / 2]) as f32 / 2.0 } else { numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // 出現回数をカウントしつつ、最大出現回数を追跡 let mut occurrences = HashMap::new(); let mut max_count = 0; for &num in numbers { let count = occurrences.entry(num).or_insert(0); *count += 1; if *count > max_count { max_count = *count; } } // 最大出現回数を持つ要素を収集 occurrences .into_iter() .filter_map(|(num, count)| if count == max_count { Some(num) } else { None }) .collect()}
konishu
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { if numbers.len() % 2 == 0 { return (numbers[numbers.len() / 2 + 1] + numbers[numbers.len() / 2]) as f32 / 2.0; } else { return numbers[numbers.len() / 2] as f32; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut occurrences = std::collections::HashMap::new(); for &num in numbers { let count = occurrences.entry(num).or_insert(0); *count += 1; } let max_count = occurrences.values().cloned().max().unwrap_or(0); let mut result = Vec::new(); for (&num, &count) in &occurrences { if count == max_count { // max_count = count; // max_num = num; result.push(num); } } result}
jeypiti
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 len = numbers.len(); if len % 2 == 0 { (numbers[len / 2] + numbers[len / 2 - 1]) as f32 / 2.0 } else { numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut counts: HashMap<i32, u32> = HashMap::new(); //numbers.into_iter().map(|&key| (key, 0)).collect(); for &n in numbers { *counts.entry(n).or_insert(0) += 1; } let max = counts.values().max().expect("there should be a max"); let mut res: Vec<i32> = counts.iter().filter_map(|(&key, val)| if val == max { Some(key) } else { None }).collect(); res.sort(); res}
0xsmarter
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); if n % 2 == 0 { (numbers[n / 2 - 1] + numbers[n / 2]) as f32 / 2.0 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut f = HashMap::new(); for x in numbers { f.entry(x).and_modify(|v| *v += 1).or_insert(1); } let mx_f = f.values().into_iter().max().unwrap(); let mut modes = Vec::new(); for (&n, cnt) in &f { if *cnt == *mx_f { modes.push(*n); } } modes.sort(); modes}
aynugek
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); match numbers.len() { 0 => panic!("vector cannot be empty"), 1 => numbers[0] as f32, len => { let mid = len / 2; if len % 2 == 0 { (numbers[mid - 1] as f32 + numbers[mid] as f32) / 2.0 } else { numbers[mid] as f32 } } }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let frequency_map = numbers .iter() .fold(HashMap::new(), |mut h, &num| { h.entry(num).and_modify(|c| *c += 1).or_insert(0); h }); let max_frequency = frequency_map.values().max().unwrap_or(&0); let mut modes: Vec<_> = frequency_map .iter() .filter(|(_, freq)| *freq == max_frequency) .map(|(num, _)| *num) .collect(); modes.sort(); modes}
cip999
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); ((numbers[(n - 1) / 2] + numbers[n / 2]) as f32) / 2_f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut frequency: HashMap<i32, i32> = HashMap::new(); let mut max_freq = 0; numbers.iter().for_each(|&x| { max_freq = std::cmp::max( max_freq, *frequency.entry(x).and_modify(|f| *f += 1).or_insert(1) ) }); let mut modes = frequency .into_iter() .filter_map(|(x, f)| if f == max_freq { Some(x) } else { None }) .collect::<Vec<_>>(); modes.sort(); modes}
tsucchinoko
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); if n % 2 == 0 { (numbers[n / 2 - 1] + numbers[n / 2]) as f32 / 2.0 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut f = HashMap::new(); for x in numbers { f.entry(x).and_modify(|v| *v += 1).or_insert(1); } let mx_f = f.values().into_iter().max().unwrap(); let mut modes = Vec::new(); for (&n, cnt) in &f { if *cnt == *mx_f { modes.push(*n); } } modes.sort(); modes}
ankeetparikh
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); if n % 2 == 0 { (numbers[n / 2 - 1] + numbers[n / 2]) as f32 / 2.0 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut f = HashMap::new(); for x in numbers { f.entry(x).and_modify(|v| *v += 1).or_insert(1); } let mx_f = f.values().into_iter().max().unwrap(); let mut modes = Vec::new(); for (&n, cnt) in &f { if *cnt == *mx_f { modes.push(*n); } } modes.sort(); modes}
sassy
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); if len % 2!= 0 { numbers[(len - 1) / 2] as f32 } else { (numbers[len / 2 - 1] + numbers[len / 2]) as f32 / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut values = HashMap::new(); for number in numbers { let count = values.entry(number).or_insert(0); *count += 1; } let mut max = 0; let mut result :Vec<i32> = vec!(0); for (&key, &value) in values.iter() { if value > max { max = value; result = vec!(*key); } else if max == value { result.push(*key); } } result}
Rolando1994
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 index = numbers.len() / 2; if numbers.len() % 2 == 0 { return (numbers[index-1] + numbers[index]) as f32 / 2.0; } else { return numbers[index] as f32; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut map = HashMap::new(); let mut max = 0; let mut mode: Vec<i32> = Vec::new(); for &x in numbers { let count = map.entry(x).or_insert(0); *count += 1; if *count > max { max = *count; mode = vec![x]; } else if *count == max { mode.push(x); } } mode}
maxvi
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let mid = numbers.len() / 2; if numbers.len() % 2 == 0 { (numbers[mid] + numbers[mid - 1]) as f32 / 2.0 } else { numbers[mid] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map = HashMap::new(); let mut max = 0; let mut modes = Vec::new(); for &x in numbers { let count = map.entry(x).or_insert(0); *count += 1; if *count > max { max = *count; modes = vec![x]; } else if *count == max { modes.push(x); } } modes}
diegocmsantos
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); if numbers.len() % 2 == 0 { (numbers[len / 2 - 1] as f32 + numbers[len / 2] as f32) / 2.0 } else { numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut hmap = HashMap::new(); for &n in numbers { *hmap.entry(n).or_insert(0) += 1; } let mut max_frequency = 0; for &value in hmap.values() { if max_frequency < value { max_frequency = value; } } let mut res = Vec::new(); for (&key, &value) in &hmap { if value == max_frequency { res.push(key); } } res}
senft
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let idx: usize = numbers.len() / 2; if numbers.len() % 2 == 0 { (numbers[idx] + numbers[idx - 1]) as f32 / 2.0 } else { numbers[idx] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts = HashMap::new(); let mut max_freq = 0; for number in numbers { counts.entry(*number).and_modify(|n| *n += 1).or_insert(1); max_freq = std::cmp::max(max_freq, counts[number]); } let mut result = vec![]; for (number, count) in counts.into_iter() { if count == max_freq { result.push(number); } } result.sort(); result}
whitwulf
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 len = numbers.len(); if numbers.len() % 2 == 0 { (numbers[len / 2 - 1] as f32 + numbers[len / 2] as f32) / 2.0 } else { numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut hmap = HashMap::new(); for &n in numbers { *hmap.entry(n).or_insert(0) += 1; } let mut max_frequency = 0; for &value in hmap.values() { if max_frequency < value { max_frequency = value; } } let mut res = Vec::new(); for (&key, &value) in &hmap { if value == max_frequency { res.push(key); } } res}
anthonycabreralara
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let length = numbers.len(); if length % 2 == 0 { let mid1 = numbers[length / 2]; let mid2 = numbers[length / 2 - 1]; return (mid1 as f32 + mid2 as f32) / 2.0; } numbers[length / 2] as f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut number_frequency = HashMap::new(); for &number in numbers { *number_frequency.entry(number).or_insert(0) += 1; } let mut max_frequency = 0; for &value in number_frequency.values() { if max_frequency < value { max_frequency = value; } } let mut result = Vec::new(); for (&key, &value) in &number_frequency { if value == max_frequency { result.push(key); } } result}
cloki0610
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { // TODO: Implement logic here to return the median of the list if numbers.is_empty() { return 0.0; } numbers.sort(); let len = numbers.len(); if len % 2 == 0 { (numbers[len / 2] + numbers[len / 2 - 1]) as f32 / 2.0 } else { numbers[len/2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut occurrences = HashMap::new(); for &number in numbers { *occurrences.entry(number).or_insert(0) += 1; } let max_frequency = occurrences.values().cloned().max().unwrap_or(0); let mut modes: Vec<i32> = occurrences .into_iter() .filter(|&(_, freq)| freq == max_frequency) .map(|(num, _)| num) .collect(); modes.sort(); modes}
sergepin
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); if len % 2 == 0 { let median1 = numbers[len/2]; let median2 = numbers[len/2 -1]; (median1 as f32 + median2 as f32)/2.0 }else{ numbers[len/2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut ocurrences = HashMap::new(); for &num in numbers{ *ocurrences.entry(num).or_insert(0) += 1; } let max_freq = ocurrences.values().cloned().max().unwrap_or(0); let mut modes: Vec<i32> = ocurrences .into_iter() .filter(|&(_, freq)| freq == max_freq) .map(|(num, _)| num) .collect(); modes.sort(); modes}
igroomgrim
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); // Sort the numbers let mid = numbers.len() / 2; // Find the middle index // Check if the length of the list is even or odd if numbers.len() % 2 == 0 { // If even, return the average of the two middle numbers (numbers[mid - 1] + numbers[mid]) as f32 / 2.0 } else { // If odd, return the middle number numbers[mid] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut occurrences = HashMap::new(); // Create a HashMap to count occurrences let mut max_count = 0; // Variable to track the maximum count let mut modes = Vec::new(); // Vector to store the mode(s) // Count occurrences of each number for &number in numbers { let count = occurrences.entry(number).or_insert(0); *count += 1; // Update max_count and modes if *count > max_count { max_count = *count; modes.clear(); // Clear previous modes modes.push(number); // Add new mode } else if *count == max_count { modes.push(number); // Add to modes if it matches max_count } } modes.sort(); // Sort the modes for consistency modes.dedup(); // Remove duplicates if any modes // Return the mode(s)}
Tallere
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); if n % 2 != 0 { numbers[n / 2] as f32 } else { (numbers[(n - 1) / 2] + numbers[n / 2]) as f32 / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counter : HashMap<i32, usize> = HashMap::new(); for e in numbers { let old_val : usize = counter.get(e).cloned().unwrap_or(0); counter.insert(e.clone(), old_val + 1); } let max_val_opt : Option<usize> = counter.values().max().cloned(); if let Some(max_val) = max_val_opt { let mut res = Vec::new(); for (k, v) in counter.into_iter() { if v == max_val { res.push(k) } } res.sort(); res } else { vec![] }}
RisaI
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() % 2 == 0 { (numbers[numbers.len() / 2] + numbers[numbers.len() / 2 - 1]) as f32 / 2. } else { numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts = HashMap::new(); numbers.iter().copied().for_each(|v| { *counts.entry(v).or_default() += 1; }); counts .into_iter() .fold((0, vec![]), |(mut prev_count, mut prev), (next, count)| { if prev_count < count { prev_count = count; prev.clear(); } if prev_count == count { prev.push(next); } (prev_count, prev) }) .1}
StimhackSoftware
use std::collections::HashMap;use std::ops::Rem;use std::ops::Div;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() >= 2 { if numbers.len().rem(2) == 0 { let piv_1 = numbers[numbers.len().div(2)] as f32; let piv_2 = numbers[numbers.len().div(2) - 1] as f32; (piv_1 + piv_2) / 2.0 } else { let pivot = numbers.len().div(2); numbers[pivot] as f32 } } else { if numbers.len() == 1 { numbers[0] as f32 } else { -1.0 } } }pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counter : HashMap<i32, usize> = HashMap::new(); for e in numbers { let old_val : usize = counter.get(e).cloned().unwrap_or(0); counter.insert(e.clone(), old_val + 1); } let max_val_opt : Option<usize> = counter.values().max().cloned(); if let Some(max_val) = max_val_opt { let mut res = Vec::new(); for (k, v) in counter.into_iter() { if v == max_val { res.push(k) } } res.sort(); res } else { vec![] } }
StimhackSoftware
use std::collections::HashMap;use std::ops::Rem;use std::ops::Div;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() >= 2 { if numbers.len().rem(2) == 0 { let piv_1 = numbers[numbers.len().div(2)] as f32; let piv_2 = numbers[numbers.len().div(2) - 1] as f32; (piv_1 + piv_2) / 2.0 } else { let pivot = numbers.len().div(2); numbers[pivot] as f32 } } else { if numbers.len() == 1 { numbers[0] as f32 } else { -1.0 } } }pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counter = HashMap::new(); for e in numbers { let old_val : i32 = counter.get(e).cloned().unwrap_or(0); counter.insert(e.clone(), old_val + 1); } let max_val_opt : Option<&i32> = counter.values().max(); if let Some(max_val) = max_val_opt { let mut res = Vec::new(); for (k, v) in counter.iter() { if *v == *max_val { res.push(*k) } } res.sort(); res } else { vec![] } }
TeemuSailynoja
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() % 2 != 0 { numbers[numbers.len() / 2] as f32 } else { 0.5 * numbers[(numbers.len() / 2 - 1)..=(numbers.len() / 2)].iter().sum::<i32>() as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts : HashMap<i32, usize> = HashMap::new(); let mut max_val: usize = 0; numbers.iter().for_each(|n| { let count = counts.entry(*n).and_modify(|v| {*v += 1;}).or_insert(1); if *count > max_val { max_val = *count; } } ); let mut res = counts.iter().filter(|(&_k,&v)| v == max_val).map(|(k,_v)| *k).collect::<Vec<i32>>(); res.sort(); res}
sml7
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let length = numbers.len(); if length % 2 == 0{ return ((numbers[length / 2 - 1] + numbers[length / 2]) as f32 ) /2 as f32; } else { return numbers[length / 2] as f32; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {let mut occurrences = HashMap::new();for &num in numbers{ *occurrences.entry(num).or_insert(0) += 1; }let max_count = occurrences.values().cloned().max().unwrap_or(0);let mut modes: Vec<i32> = occurrences.into_iter().filter(|&(_, count)| count == max_count).map(|(num, _)| num).collect();modes.sort();modes}
armed
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let cnt = numbers.len(); if cnt % 2 == 0 { let l = cnt / 2; return (numbers[l - 1] + numbers[l]) as f32 / 2.0 } return numbers[(cnt - 1) / 2] as f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut hm = HashMap::new(); let mut result: Vec<i32>; let mut max = 0; for &n in numbers { hm.entry(n).and_modify(|v| { *v += 1; if *v > max { max = *v; } }).or_insert(1); } hm.retain(|_, v| *v == max); result = hm.keys().cloned().collect(); result.sort(); result}
shinobu52
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let length = numbers.len(); if length % 2 == 0 { return ((numbers[length / 2 - 1] + numbers[length / 2]) as f32) / 2 as f32; } else { return numbers[length / 2] as f32; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut occurrences = HashMap::new(); for &num in numbers { *occurrences.entry(num).or_insert(0) += 1; } let max_count = occurrences.values().cloned().max().unwrap_or(0); let mut modes: Vec<i32> = occurrences .into_iter() .filter(|&(_, count)| count == max_count) .map(|(num, _)| num) .collect(); modes.sort(); modes}
sio-funmatsu
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { // TODO: Implement logic here to return the median of the list if numbers.is_empty() { return 0.0; } let mut nums = numbers.clone(); nums.sort(); if nums.len() % 2 == 0 { (nums[nums.len() / 2 - 1] as f32 + nums[nums.len() / 2] as f32) / 2.0 } else { nums[nums.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list if numbers.is_empty() { return vec![]; } let nums = numbers.iter().fold(HashMap::new(), |mut acc, &i| { *acc.entry(i).or_insert(0) += 1; acc }); let max = *nums.values().max().unwrap(); let mut res: Vec<i32> = nums.into_iter() .filter(|(_, v)| *v == max) .map(|(k, _)| k) .collect(); res.sort(); res}
GiuleXje
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let mid = numbers.len() / 2; if numbers.len() % 2 == 0 { return (numbers[mid - 1] + numbers[mid]) as f32 / 2.0; } else { return numbers[mid] as f32; };}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut sorted = numbers.clone(); sorted.sort(); let mut modemap = HashMap::new(); for &number in &sorted { modemap.entry(number).and_modify(|number| *number += 1).or_insert(1); } let key_with_max_value = modemap.iter().max_by_key(|entry | entry.1).unwrap(); let mut mode = Vec::new() ; for (key, val) in modemap.iter() { if val == key_with_max_value.1{ mode.push(*key); } } mode.sort(); return mode;}
CaelReader
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let mid = numbers.len() / 2; if numbers.len() % 2 == 0 { return (numbers[mid - 1] + numbers[mid]) as f32 / 2.0; } else { return numbers[mid] as f32; };}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut sorted = numbers.clone(); sorted.sort(); let mut modemap = HashMap::new(); for &number in &sorted { modemap.entry(number).and_modify(|number| *number += 1).or_insert(1); } let key_with_max_value = modemap.iter().max_by_key(|entry | entry.1).unwrap(); let mut mode = Vec::new() ; for (key, val) in modemap.iter() { if val == key_with_max_value.1{ mode.push(*key); } } mode.sort(); return mode;}
i5-650
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { if numbers.is_empty() { return 0.0; } let len = numbers.len(); if len % 2 == 0 { (numbers[len / 2] + numbers[len / 2 + 1]) as f32 / 2.0 } else { numbers[len/2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut occurrences = HashMap::new(); for &number in numbers { *occurrences.entry(number).or_insert(0) += 1; } let max_frequency = occurrences.values().cloned().max().unwrap_or(0); let mut modes: Vec<i32> = occurrences .into_iter() .filter(|&(_, freq)| freq == max_frequency) .map(|(num, _)| num) .collect(); modes.sort(); modes}
its-me-sv
pub fn median(numbers: &mut [i32]) -> f32 { // TODO: Implement logic here to return the median of the list numbers.sort(); let length = numbers.len(); let middle = length / 2; match length % 2 != 0 { true => numbers[middle] as f32, false => (numbers[middle] + numbers[middle - 1]) as f32 / 2., }}pub fn mode(numbers: &[i32]) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut max_freq = i32::MIN; let counter = numbers .iter() .fold(std::collections::HashMap::new(), |mut acc, x| { let freq = acc .entry(x) .and_modify(|counter| *counter += 1) .or_insert(1); max_freq = max_freq.max(*freq); acc }); let mut modes = counter .into_iter() .filter_map(|(&number, freq)| (freq == max_freq).then_some(number)) .collect::<Vec<i32>>(); modes.sort(); modes}
its-me-sv
pub fn median(numbers: &mut [i32]) -> f32 { // TODO: Implement logic here to return the median of the list numbers.sort(); let length = numbers.len(); let middle = length / 2; match length % 2 != 0 { true => numbers[middle] as f32, false => (numbers[middle] + numbers[middle - 1]) as f32 / 2., }}pub fn mode(numbers: &[i32]) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut max_freq = i32::MIN; let counter = numbers .iter() .fold(std::collections::HashMap::new(), |mut acc, x| { let freq = acc .entry(x) .and_modify(|counter| *counter += 1) .or_insert(1); max_freq = max_freq.max(*freq); acc }); let mut modes = counter .into_iter() .filter(|(_, freq)| freq == &max_freq) .map(|(&number, _)| number) .collect::<Vec<i32>>(); modes.sort(); modes}
raneid
use std::collections::{ HashMap, HashSet };pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let n = numbers.len(); let mut res = *numbers.get(n / 2).unwrap() as f32; if n % 2 == 0 { res += *numbers.get(n / 2 - 1).unwrap() as f32; res /= 2.0; } res}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut histogram: HashMap<i32, usize> = HashMap::new(); let mut top: HashSet<i32> = HashSet::new(); let mut count = 0; if numbers.len() == 0 { return vec![]; } for &i in numbers.iter() { let v = histogram.entry(i).or_insert(0); *v += 1; if *v > count { top.clear(); count = *v; } if *v >= count { top.insert(i); } } let mut result: Vec<i32> = vec![]; for i in top.iter() { result.push(*i); } result.sort(); result}
MegaThorx
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); match numbers.len() % 2 == 0 { true => (numbers[numbers.len() / 2 - 1] + numbers[numbers.len() / 2]) as f32 / 2.0, false => numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts = HashMap::<i32, usize>::new(); for number in numbers { counts.entry(*number).and_modify(|counter| *counter += 1).or_insert(1); } let (_, max_count) = counts.iter().max_by_key(|(_, v)| *v).unwrap(); counts.iter().filter(|(_, v)| *v == max_count).map(|(k, _)| *k).collect()}
mrsalo
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); match numbers.len() { 0 => f32::NAN, x if x % 2 == 0 => { let (a, b) = (numbers[x/2 - 1], numbers[x/2]); (a as f32 + b as f32) / 2. } x => { numbers[x/2] as f32 } }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { use std::iter; let mut counts = HashMap::<i32, usize>::new(); for i in numbers.iter() { counts.entry(*i).and_modify(|counter| *counter += 1).or_insert(1); } let (_, maxtimes) = counts.iter().max_by_key(|(_,v)| **v).unwrap(); counts.iter() .filter(|(_k, v)| *v == maxtimes) .map(|(k, _v)| *k) .collect()}
josedorrego
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 { (numbers[(numbers.len()/2)-1] + numbers[numbers.len()/2]) as f32 / 2.0 }else{ numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut mode: Vec<i32> = Vec::new(); let mut repeats: HashMap<i32, i32> = HashMap::new(); for i in numbers { let count = repeats.entry(*i).or_insert(0); *count += 1; } let mut max_repeat = 0; for (num, repeat) in &repeats { if *repeat >= max_repeat { if *repeat == max_repeat { mode.push(*num) } else { mode = Vec::new(); mode.push(*num); max_repeat = *repeat; } } } mode.sort(); mode}
uggla
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 { (*numbers.get(numbers.len()/2 - 1).unwrap() + *numbers.get(numbers.len()/2).unwrap()) as f32 / 2.0 } else { *numbers.get(numbers.len()/2).unwrap() as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut hashmap: HashMap<i32, i32> = HashMap::new(); numbers.iter().for_each(|number| *hashmap.entry(*number).or_insert(0) += 1); let max = *hashmap.iter().max_by(|a,b| a.1.cmp(&b.1)).unwrap().1; hashmap.iter().filter(|o| *o.1 == max).map(|o| *o.0).collect()}
ludovicknecht
use std::collections::HashMap;pub fn median(numbers: &Vec<i32>) -> f32 { // Imo median should be a read operation and not alter the collection // (no context given here though) let mut vec = numbers.clone(); vec.sort(); let len = vec.len(); match len % 2 == 0 { true => (vec[len/2 - 1] + vec[len/2]) as f32 / 2.0, false => vec[len/2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut occurrences = HashMap::<i32, i32>::new(); for &number in numbers { occurrences.entry(number) .and_modify(|count| *count += 1) .or_insert(1); } let mut vec = Vec::<i32>::new(); for (&key, &val) in occurrences.iter() { if vec.len() == 0 { // No value pushed yet, push the current one vec.push(key); } else if val > *occurrences.get(&vec[0]).unwrap() { // New value has more occurrences than previous ones // Only keep this new value vec = Vec::new(); vec.push(key); } else if val == *occurrences.get(&vec[0]).unwrap() { // New value has as many occurrences as previous ones // Add this new value to kept ones vec.push(key); } } vec.sort(); vec}
saukmn
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 mid = numbers.len() / 2; if numbers.len() % 2 == 0 { let a = *numbers.get(mid - 1).unwrap() as f32; let b = *numbers.get(mid).unwrap() as f32; (a + b) / 2.0 } else { *numbers.get(mid).unwrap() as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut frequencies = HashMap::<i32, i32>::new(); numbers.iter().for_each(|num| *frequencies.entry(*num).or_insert(0) += 1); let max = *frequencies.values().max().unwrap(); frequencies.iter().filter(|e| e.1 == &max).map(|e| *e.0).collect()}