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.ad0x99
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { // Sort the numbers numbers.sort(); let len = numbers.len(); if len == 0 { panic!("Cannot calculate median of an empty list"); } if len % 2 == 1 { // If odd length, then return the middle element numbers[len / 2] as f32 } else { // If even length, then return the average of the two middle elements let mid1 = numbers[len / 2 - 1]; let mid2 = numbers[len / 2]; (mid1 + mid2) as f32 / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut frequency = HashMap::new(); for &num in numbers { *frequency.entry(num).or_insert(0) += 1; } let max_frequency = frequency.values().cloned().max().unwrap_or(0); frequency .into_iter() .filter(|&(_, freq)| freq == max_frequency) .map(|(num, _)| num) .collect()}
fdumontmd
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let l = numbers.len(); if l % 2 == 0 { (numbers[l / 2] as f32 + numbers[l / 2 - 1] as f32) / 2.0 } else { numbers[l / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut count = HashMap::new(); for n in numbers { count.entry(n.clone()).and_modify(|c| *c += 1).or_insert(1); } if let Some(max) = count.values().max().cloned() { let mut r: Vec<_> = count.into_iter().filter_map(|(n, c)| if c == max { Some(n) } else { None }).collect(); r.sort(); r } else { Vec::new() }}
sander-b-postnl
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { if numbers.is_empty() { return 0.0; } numbers.sort(); let len = numbers.len(); if len % 2 == 0 { let mid1 = numbers[len / 2 - 1]; let mid2 = numbers[len / 2]; (mid1 + mid2) 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(); } let mut counts = HashMap::new(); for &num in numbers { *counts.entry(num).or_insert(0) += 1; } let max_count = counts.values().cloned().max().unwrap_or(0); counts .into_iter() .filter(|&(_, count)| count == max_count) .map(|(num, _)| num) .collect()}
savasozturk
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); let mid = (len as f32 / 2.0) as usize; if 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 nums: HashMap<i32, i32> = HashMap::new(); for &i in numbers { *nums.entry(i).or_insert(0) += 1; } // Find the maximum frequency let max_count = nums.values().cloned().max().unwrap_or(0); let mut result = nums .iter() .filter(|(_, &v)| v == max_count) .map(|(&k, _)| k) .collect::<Vec<i32>>(); result.sort(); result}
zavakid
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; // Return 0.0 if the list is empty } numbers.sort(); // Sort the list in ascending order let len = numbers.len(); if len % 2 == 0 { // Even length: average of two middle numbers let mid1 = numbers[len / 2 - 1]; let mid2 = numbers[len / 2]; (mid1 + mid2) as f32 / 2.0 } else { // Odd length: middle number numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.is_empty() { return Vec::new(); // Return an empty vector if the list is empty } let mut counts = HashMap::new(); // Count occurrences of each number for &num in numbers { *counts.entry(num).or_insert(0) += 1; } // Find the maximum frequency let max_count = counts.values().cloned().max().unwrap_or(0); // Collect all numbers with the maximum frequency counts .into_iter() .filter(|&(_, count)| count == max_count) .map(|(num, _)| num) .collect()}
mk-comm
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { if numbers.is_empty() { return 0.0; // Return 0.0 if the list is empty } numbers.sort(); // Sort the list in ascending order let len = numbers.len(); if len % 2 == 0 { // Even length: average of two middle numbers let mid1 = numbers[len / 2 - 1]; let mid2 = numbers[len / 2]; (mid1 + mid2) as f32 / 2.0 } else { // Odd length: middle number numbers[len / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.is_empty() { return Vec::new(); // Return an empty vector if the list is empty } let mut counts = HashMap::new(); // Count occurrences of each number for &num in numbers { *counts.entry(num).or_insert(0) += 1; } // Find the maximum frequency let max_count = counts.values().cloned().max().unwrap_or(0); // Collect all numbers with the maximum frequency counts .into_iter() .filter(|&(_, count)| count == max_count) .map(|(num, _)| num) .collect()}
carlos-quantexai
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { // Sort the numbers to find the median numbers.sort(); // Added sorting logic let len = numbers.len(); let median: f32; if len % 2 == 0 { median = (numbers[len / 2 - 1] + numbers[len / 2]) as f32 / 2.0; // Calculate median for even length } else { median = numbers[len / 2] as f32; // Calculate median for odd length } median // Return the median}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { use std::collections::HashMap; let mut occurrences = HashMap::new(); // Create a HashMap to count occurrences for &num in numbers.iter() { *occurrences.entry(num).or_insert(0) += 1; // Count each number } let max_count = occurrences.values().cloned().max().unwrap_or(0); // Find the maximum count occurrences.into_iter() .filter(|&(_, count)| count == max_count) // Filter numbers with max count .map(|(num, _)| num) // Map to the numbers .collect() // Collect into a Vec<i32>}
majesticalcreature
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let mut copy: Vec<&i32> = numbers.iter().collect(); copy.sort(); let mid = (copy.len() as f32 / 2.0) - 1.0; if copy.len() % 2 == 0 { let i = mid.floor() as usize; return (*copy[i] + *copy[i + 1]) as f32 / 2.0; } *copy[mid.round() as usize] as f32}struct ModeList { nums: Vec<i32>, quantity: usize}impl ModeList { fn new() -> ModeList { ModeList{ nums: Vec::new(), quantity: 0 } } fn add(&mut self, number: i32, quantity: usize) { if quantity > self.quantity { self.nums.clear(); self.nums.push(number); self.quantity = quantity; } else if quantity == self.quantity { self.nums.push(number); } }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map: HashMap<i32, usize> = HashMap::new(); for n in numbers { map.insert(*n, match map.get(n) { Some(q) => q + 1, None => 1 }); } let mut modelist = ModeList::new(); for (n, q) in map { modelist.add(n, q); } modelist.nums.sort(); modelist.nums}
jose-bernardo
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 { (numbers[mid] + numbers[mid - 1]) as f32 / 2.0 } else { numbers[mid] 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, i32> = HashMap::new(); let mut max = 0; numbers.iter() .for_each(|&n| { max = max.max( *map.entry(n) .and_modify(|value| *value += 1) .or_insert(1)); }); let mut result = map.iter() .filter(|(_, &v)| v == max) .map(|(&k, _)| k) .collect::<Vec<i32>>(); result.sort(); result}
Mxn-ptr
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 m1 = numbers[numbers.len() / 2]; let m2 = numbers[numbers.len() / 2 - 1]; return (m1 + m2) as f32 / 2_f32; } 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 hashmap: HashMap<i32, i32> = HashMap::new(); let mut max_value = 0; let mut mode = Vec::new(); for &number in numbers { *hashmap.entry(number).or_insert(0) += 1; } for (&key, &value) in &hashmap { if value > max_value { max_value = value; mode = vec![key]; } else if value == max_value { mode.push(key); } } mode.sort(); mode}
hinphansa
use std::{collections::HashMap, vec};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 mid = numbers.len() / 2; return (numbers[mid] + numbers[mid - 1]) 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 max = 0; let mut map = HashMap::new(); for &num in numbers { let count = map.entry(num).or_insert(0); *count += 1; if *count > max { max = *count; } } let mut res = vec![]; for (key, value) in map.iter() { if *value == max { res.push(*key); } } res.sort(); return res;}
funny233-github
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 p1 = numbers[numbers.len() / 2 - 1] as f32; let p2 = numbers[numbers.len() / 2] as f32; (p1 + p2) / 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 freq: HashMap<i32, usize> = HashMap::new(); for n in numbers { freq.entry(*n).and_modify(|p| *p += 1).or_insert(1); } let mut max = 0; for (_, v) in freq.clone() { if v > max { max = v; } } let mut res = Vec::new(); for (k, v) in freq { if v == max { res.push(k); } } res}
digitalresistor
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); let m = if len % 2 == 0 { let n1 = numbers.get(len / 2).unwrap(); let n2 = numbers.get(len / 2 - 1).unwrap(); (n1 + n2) as f32 / 2_f32 } else { *(numbers.get(len / 2).unwrap()) as f32 }; m}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq: HashMap<i32, i32> = HashMap::new(); for val in numbers { freq.entry(*val).and_modify(|counter| *counter += 1).or_insert(1); }; let highest_count = freq.values().max().unwrap(); let mut highest_freq: Vec<i32> = Vec::new(); for (k, v) in &freq { if v == highest_count { highest_freq.push(*k); } } highest_freq.sort(); highest_freq}
digitalresistor
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort_by(i32::cmp); let len = numbers.len(); let m = if len % 2 == 0 { let n1 = numbers.get(len / 2).unwrap(); let n2 = numbers.get(len / 2 - 1).unwrap(); (n1 + n2) as f32 / 2_f32 } else { *(numbers.get(len / 2).unwrap()) as f32 }; m}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq: HashMap<i32, i32> = HashMap::new(); for val in numbers { freq.entry(*val).and_modify(|counter| *counter += 1).or_insert(1); }; let highest_count = freq.values().max().unwrap(); let mut highest_freq: Vec<i32> = Vec::new(); for (k, v) in &freq { if v == highest_count { highest_freq.push(*k); } } highest_freq.sort(); highest_freq}
digitalresistor
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort_by(i32::cmp); let len = numbers.len(); let m = if len % 2 == 0 { let n1 = numbers.get(len / 2).unwrap(); let n2 = numbers.get(len / 2 - 1).unwrap(); (n1 + n2) as f32 / 2_f32 } else { *(numbers.get(len / 2).unwrap()) as f32 }; m}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq: HashMap<i32, i32> = HashMap::new(); for val in numbers { freq.entry(*val).and_modify(|counter| *counter += 1).or_insert(1); }; let highest_count = freq.values().max().unwrap(); let mut highest_freq: Vec<i32> = Vec::new(); for (k, v) in &freq { if v == highest_count { highest_freq.push(*k); } } highest_freq.sort_by(i32::cmp); highest_freq}
jtruong04
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: usize = numbers.len(); if length % 2 == 0 { ((numbers[length/2] + numbers[length/2 - 1]) as f32)/2.0 } else { numbers[(length-1)/2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut entries = HashMap::new(); for &number in numbers { entries.entry(number).and_modify(|counter| *counter += 1).or_insert(1); } let max = entries.values().max(); let mut res = Vec::new(); for (key, val) in entries.iter() { if Some(val) == max { res.push(*key) } } res.sort(); res}
Aditeya
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 mid = len/2; if len % 2 == 0 { (numbers[mid] as f32 + numbers[mid-1] as f32)/2.0 } else { numbers[mid] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let len = numbers.len(); let hm = numbers.iter() .fold(HashMap::with_capacity(len), |mut acc, x| { *acc.entry(*x).or_insert(0) += 1; acc }); hm.iter().fold((Vec::with_capacity(len), i32::MIN), |mut acc, (v, c)| { if *c == acc.1 { acc.0.push(*v); } if *c > acc.1 { acc.0.clear(); acc.0.push(*v); acc.1 = *c; } acc }).0}
Aditeya
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 mid = len/2; if len % 2 == 0 { (numbers[mid] as f32 + numbers[mid-1] as f32)/2.0 } else { numbers[mid] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let len = numbers.len(); let hm = numbers.iter() .fold(HashMap::with_capacity(len), |mut acc, x| { *acc.entry(*x).or_insert(0) += 1; acc }); hm.iter().fold((Vec::with_capacity(len), i32::MIN), |mut acc, (v, c)| { if *c == acc.1 { acc.0.push(*v); } if *c > acc.1 { acc.0.clear(); acc.0.push(*v); acc.1 = *c; } acc }).0}
jhq223
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); 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> { let mut map = HashMap::new(); let mut result = vec![]; for i in numbers { map.entry(i) .and_modify(|x| { *x += 1; }) .or_insert(1); } let mx_flag = &map.values().max(); match mx_flag { Some(&x) => { for (k, v) in map { if v == x { result.push(*k); } } result.sort(); result } None => { return result; } }}
xbarnett
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] as f32 + numbers[n / 2] as f32) / 2.0 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freqs: HashMap<i32, i32> = HashMap::new(); for n in numbers { *freqs.entry(*n).or_insert(0) += 1; } let max = freqs.values().max(); let mut result = Vec::new(); for n in numbers { if freqs.get(n) == max { if !result.contains(n) { result.push(*n); } } } result.sort(); result}
ayushrawat10
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 == 0 { return 0.0; } if len % 2 == 0 { return (numbers[len / 2] + numbers[len / 2 - 1]) as f32 / 2.0; } else { return 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 freq = HashMap::new(); for &num in numbers { let count = freq.entry(num).or_insert(0); *count += 1; } let max_freq = freq.values().max().unwrap(); let mut mode = Vec::new(); for (&num, &count) in freq.iter() { if count == *max_freq { mode.push(num); } } mode.sort(); mode}
CarrotTarrot
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let length = numbers.len(); match length % 2 { 0 => (numbers[length / 2] + numbers[length / 2 + 1]) as f32 / 2., _ => numbers[length / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut number_map = HashMap::new(); let mut max_count = 0; for number in numbers { match number_map.get(number) { Some(count) => { if count + 1 > max_count { max_count = count + 1; } number_map.insert(number, count + 1); }, None => { number_map.insert(number, 1); } } } let mut result: Vec<i32> = number_map.iter() .filter(|(_k, v)| **v == max_count) .map(|(k, _v)| **k) .collect(); result.sort(); result}
CarrotTarrot
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let length = numbers.len(); match length % 2 { 0 => (numbers[length / 2] + numbers[length / 2 + 1]) as f32 / 2., _ => numbers[length / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut number_map = HashMap::new(); let mut max_count = 0; for number in numbers { match number_map.get(number) { Some(count) => { if count + 1 > max_count { max_count = count + 1; } number_map.insert(number, count + 1); }, None => { number_map.insert(number, 1); } } } let mut result: Vec<i32> = number_map.iter() .filter(|(_k, v)| **v == max_count) .map(|(k, _v)| **k) .collect(); result.sort(); result}
oneopane
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 mid = numbers.len() / 2; return (numbers[mid] + numbers[mid-1]) as f32 / 2.0; } else { let mid = numbers.len() / 2; return numbers[mid] 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, i32>::new(); for number in numbers { *map.entry(*number).or_insert(0) += 1 } let mut curr_max = 0; let mut result = Vec::<i32>::new(); for (number, count) in map { if count > curr_max { curr_max = count; result.clear(); result.push(number); } else if count == curr_max { result.push(number); } } result.sort(); result}
amassare
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(); if length&1==0{ (numbers[length>>1] as f32+numbers[(length>>1)-1] as f32)/2.0 }else{ numbers[length>>1] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut counter=HashMap::new(); for num in numbers.iter(){ counter.entry(num).and_modify(|num|*num+=1).or_insert(1); } let mut res=vec![]; let mut max_cnt=usize::MIN; for (k,v) in counter{ if v>max_cnt{ max_cnt=v; res=vec![*k]; }else if v==max_cnt{ res.push(*k); } } res.sort(); res}
CianciuStyles
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 mid = numbers.len() / 2; return (numbers[mid] + numbers[mid-1]) as f32 / 2.0; } else { let mid = numbers.len() / 2; return numbers[mid] 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, i32>::new(); for number in numbers { *map.entry(*number).or_insert(0) += 1 } let mut curr_max = 0; let mut result = Vec::<i32>::new(); for (number, count) in map { if count > curr_max { curr_max = count; result.clear(); result.push(number); } else if count == curr_max { result.push(number); } } result.sort(); result}
morigs
use std::collections::HashMap;pub fn median(n: &mut Vec<i32>) -> f32 { n.sort_unstable(); let (qe, rem) = (n.len() / 2, n.len() % 2); (n[qe + (rem) - 1] + n[qe]) as f32 / 2.0}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { if numbers.len() == 0 { return vec![]; } let mut freq = HashMap::new(); let mut max = numbers[0]; for n in numbers { freq.entry(n).and_modify(|counter| *counter += 1).or_insert(1); if freq[n] > max { max = freq[n]; } } let mut max = freq.iter() .filter(|(_, v)| **v == max) .map(|(k, _)| **k) .collect::<Vec<i32>>(); max.sort_unstable(); max}
kyhou
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let middle = numbers.len() / 2; if numbers.len() % 2 == 0 { (numbers[middle - 1] + numbers[middle]) as f32 / 2.0 } else { numbers[middle] as f32 }}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(0) += 1; } let mut max = 0; let mut modes = Vec::new(); for (&key, &value) in &map { match value.cmp(&max) { std::cmp::Ordering::Greater => { max = value; modes = vec![key]; } std::cmp::Ordering::Equal => { modes.push(key); } _ => {} } } modes.sort(); modes}
masteryachty
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(); if n % 2 == 0 { (numbers[n / 2] as f32 + numbers[n / 2 - 1] as f32) / 2f32 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq_map = HashMap::new(); for &number in numbers { *freq_map.entry(number).or_insert(0) += 1; } let max_freq = freq_map.values().copied().max().unwrap_or(0); let mut modes: Vec<i32> = freq_map .into_iter() .filter(|&(_, count)| count == max_freq) .map(|(num, _)| num) .collect(); modes.sort(); modes}
damascussteel21
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(); if n % 2 == 0 { (numbers[n / 2] as f32 + numbers[n / 2 - 1] as f32) / 2f32 } else { numbers[n / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut md = Vec::new(); let mut hmap = HashMap::new(); let mut max = 0; for n in numbers.iter() { *hmap.entry(n).or_insert(0) += 1; if *hmap.get(n).unwrap() > max { max = *hmap.get(n).unwrap(); } } for n in hmap.keys() { if *hmap.get(n).unwrap() == max { md.push(**n); } } md.sort(); md}
wischi-chr
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); let half = len / 2; if len % 2 == 1 { numbers[half] as f32 } else { (numbers[half] + numbers[half - 1]) as f32 / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq_map = HashMap::new(); for &number in numbers { *freq_map.entry(number).or_insert(0) += 1; } let max_freq = freq_map.values().copied().max().unwrap_or(0); let mut modes: Vec<i32> = freq_map .into_iter() .filter(|&(_, count)| count == max_freq) .map(|(num, _)| num) .collect(); modes.sort(); modes}
jaswgreen
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 index = numbers.len() / 2; return numbers[index as usize] as f32; } else { let index1: usize = numbers.len() / 2 - 1; let index2: usize = numbers.len() / 2; return (numbers[index1] + numbers[index2]) as f32 / 2.0; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut counts = HashMap::new(); for &number in numbers { *counts.entry(number).or_insert(0) += 1; } let max_value = counts.values().max().unwrap(); counts.iter() .filter(|(_k, v)| v == &max_value) .map(|(k, _)| k.clone()) .collect()}
Sommos
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 mid = len / 2; match len % 2 { 1 => numbers[mid] as f32, _ => (numbers[mid - 1] as f32 + numbers[mid] as f32) / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut counts = HashMap::new(); for &number in numbers { *counts.entry(number).or_insert(0) += 1; } let max_value = counts.values().max().unwrap(); counts.iter() .filter(|(_k, v)| v == &max_value) .map(|(k, _)| k.clone()) .collect()}
elhananby
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let len = numbers.len(); let mid = len / 2; match len % 2 { 1 => numbers[mid] as f32, _ => (numbers[mid-1] as f32 + numbers[mid] as f32) / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts = HashMap::new(); for &number in numbers { *counts.entry(number).or_insert(0) += 1; } let max_value = counts.values().max().unwrap(); counts.iter() .filter(|(_k, v)| v == &max_value) .map(|(k, _)| k.clone()) .collect()}
elhananby
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() % 2 != 0 { let index = numbers.len() / 2; return numbers[index as usize] as f32; } else { let index1: usize = numbers.len() / 2 - 1; let index2: usize = numbers.len() / 2; return (numbers[index1] + numbers[index2]) as f32 / 2.0; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut counts = HashMap::new(); for &number in numbers { *counts.entry(number).or_insert(0) += 1; } let max_value = counts.values().max().unwrap(); counts.iter() .filter(|(_k, v)| v == &max_value) .map(|(k, _)| k.clone()) .collect()}
hilias
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] + numbers[numbers.len() / 2 - 1]) 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 numbers = numbers.to_vec(); numbers.sort(); let mut frequencies = HashMap::new(); let mut highest_frequency = 0; for n in numbers { let freq = frequencies.entry(n).and_modify(|counter| *counter += 1).or_insert(1); if *freq > highest_frequency { highest_frequency = *freq; } } let mut resp = Vec::new(); for (&k, &v) in frequencies.iter() { if v == highest_frequency { resp.push(k); } } resp}
qcabanes-hobby
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let n = numbers.len(); numbers.sort(); if n & 1 == 0 { return (numbers[n / 2 - 1] + numbers[n / 2]) as f32 / 2.0; } else { return numbers[n / 2] as f32; }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut m = HashMap::<i32, i32>::new(); for n in numbers { m.entry(*n).and_modify(|x| *x += 1).or_insert(1); } let max = m.iter().max_by(|a, b| a.1.cmp(&b.1)).map(|(_, v)| v).unwrap(); let mut result = m.iter().filter(|a| a.1 == max).map(|a| *a.0).collect::<Vec<i32>>(); result.sort(); result}
vineelkovvuri
pub fn median(numbers: &mut Vec<i32>) -> f32 { // TODO: Implement logic here to return the median of the list if numbers.len() % 2 == 0 { let mid = numbers.len() / 2; let median = (numbers[mid] + numbers[mid + 1]) as f32 / 2.0; return median; } else { let mid = numbers.len() / 2; return numbers[mid] as f32; }}use std::collections::HashMap;pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map = HashMap::new(); for &num in numbers { let freq = map.entry(num).or_insert(0); *freq += 1; } let max_freq = map.values().max().unwrap(); let mut modes = Vec::<i32>::new(); for (&num, &freq) in map.iter() { if freq == *max_freq { modes.push(num); } } modes.sort(); modes}
TaiPoole
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.0 } else { numbers[(numbers.len() - 1) / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut count = HashMap::new(); let mut out = vec![]; for num in numbers { *count.entry(num).or_insert(0) += 1; } let mut max = 0; for (&num, value) in count { if value > max { out.clear(); } if value >= max { out.push(num); max = value; } } out}
TomBaston
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); if numbers.len() & 1 == 0 { 0.5 * (numbers[numbers.len() / 2 - 1] as f32 + numbers[numbers.len() / 2] as f32) } else { numbers[numbers.len() / 2] as f32 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut freq_map = HashMap::new(); for &n in numbers { let count = freq_map.entry(n).or_insert(0u32); *count += 1; } let mut result = vec![]; let mut count = 0u32; for (key, value) in freq_map { if value >= count { if value > count { result.clear() }; result.push(key); count = value; } } result.sort(); result}
swandyr
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] as f32 } else { (numbers[len / 2 - 1] + numbers[len / 2]) as f32 / 2.0 }}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 n in numbers { map.entry(n).and_modify(|v| *v += 1).or_insert(1); } let max = map.values().max().unwrap(); let mut v = map.iter().filter_map(|(k, v)| (v == max).then_some(**k)).collect::<Vec<i32>>(); v.sort(); v}
dantekelly
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let length = numbers.len(); numbers.sort(); if length % 2 != 0 { return numbers[length / 2] as f32; } return (numbers[length / 2 - 1] + numbers[length / 2]) as f32 / 2.0;}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut hash = HashMap::new(); let mut mode_freq = 0; for n in numbers { hash.entry(n).and_modify(|number| { let new_number = *number + 1; if new_number > mode_freq { mode_freq = new_number; } *number += 1 }).or_insert(1); } let mut r: Vec<i32> = vec![]; for (k, v) in hash.into_iter() { if v == mode_freq { r.push(*k); } } r.sort(); r}
dantekelly
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let length = numbers.len(); numbers.sort(); if length % 2 != 0 { return numbers[length / 2] as f32; } return (numbers[length / 2 - 1] + numbers[length / 2]) as f32 / 2.0;}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut hash = HashMap::new(); for n in numbers { hash.entry(n).and_modify(|number| *number += 1).or_insert(1); /* match hash.get(n) { Some(&number) => { hash.insert(n, number + 1); }, _ => { hash.insert(n, 1); } } */ } let mut r: Vec<i32> = vec![]; let mut mode_freq = 0; for (k, v) in hash.into_iter() { if v > mode_freq { if r.len() > 0 { r.clear(); } mode_freq = v; r.push(*k); } else if v == mode_freq { r.push(*k); } } r.sort(); r}
dantekelly
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { let length = numbers.len(); numbers.sort(); if length % 2 != 0 { return numbers[length / 2] as f32; } return (numbers[length / 2 - 1] + numbers[length / 2]) as f32 / 2.0;}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut hash = HashMap::new(); for n in numbers { match hash.get(n) { Some(&number) => { hash.insert(n, number + 1); }, _ => { hash.insert(n, 1); } } } let mut r: Vec<i32> = vec![]; let mut modeFreq = 0; for (k, v) in hash.into_iter() { if v > modeFreq { if r.len() > 0 { r.clear(); } modeFreq = v; r.push(*k); } else if v == modeFreq { r.push(*k); } } r.sort(); r}
philippthiers
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-1)/2] as f32 } else { (numbers[len/2-1] + numbers[len/2]) as f32 / 2.0 }}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { // TODO: Implement logic here to return the mode of the list let mut freqz: HashMap<i32, i32> = HashMap::new(); for i in numbers { *(freqz.entry(*i).or_insert(1)) += 1; } let mut ret: Vec<i32> = Vec::new(); let mut max = 0; for i in freqz { if i.1 > max { max = i.1; ret.clear(); ret.push(i.0); } else if i.1 == max { ret.push(i.0); } } ret.sort(); ret}
SRVng
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let length = numbers.len(); if length % 2 == 0 { let half = length / 2 - 1; let middle = &numbers[half..half + 2]; return middle.iter().sum::<i32>() as f32 / 2.; } *numbers.get(((length + 1) / 2) - 1).unwrap() as f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut map: HashMap<i32, i32> = HashMap::new(); let mut highest_count: i32 = 0; for v in numbers { map.entry(*v) .and_modify(|counter| { let new_value = *counter + 1; if new_value.gt(&&mut highest_count) { highest_count = new_value } *counter += 1; }) .or_insert(1); } let mut result: Vec<i32> = map .iter() .filter_map(|e| { if e.1.ne(&highest_count) { return None; } return Some(*e.0); }) .collect(); result.sort(); result}
jenny07007
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); 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 frequency = HashMap::new(); for &num in numbers { *frequency.entry(num).or_insert(0) += 1; } let max_frequency = frequency.values().cloned().max().unwrap_or(0); let mut modes: Vec<i32> = frequency .into_iter() .filter(|&(_, count)| count == max_frequency) .map(|(num, _)| num) .collect(); modes.sort(); modes}
Songiso
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let size = numbers.len(); if size % 2 == 0 { return (numbers[size / 2 - 1] + numbers[size / 2]) as f32 / 2. } numbers[size / 2] as f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut max = 0; let mut map = HashMap::new(); for &number in numbers.iter() { let e = *map.entry(number).and_modify(|counter| *counter += 1).or_insert(1); if e > max { max = e; } } let mut result = vec![]; for (&key, &value) in map.iter() { if value == max { result.push(key); } } result}
rajaomariajaona
use std::collections::HashMap;pub fn median(numbers: &mut Vec<i32>) -> f32 { numbers.sort(); let size = numbers.len(); if size % 2 == 0 { return (numbers[size / 2 - 1] + numbers[size / 2]) as f32 / 2. } numbers[size / 2] as f32}pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { let mut max = 0; let mut map = HashMap::new(); for &number in numbers.iter() { let e = *map.entry(number).and_modify(|counter| *counter += 1).or_insert(1); if e > max { max = e; } } let mut result = vec![]; for (&key, &value) in map.iter() { if value == max { result.push(key); } } result}
HeavyMetalGeek
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(); if n % 2 == 0 { let mid = n/2; (numbers[mid - 1] + numbers[mid]) as f32 / 2.0 } else { numbers[n/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 i in numbers.iter() { map.entry(*i).and_modify(|count| *count += 1).or_insert(1); } let mut nums = Vec::new(); let mut max = 0; for (k,v) in map.iter() { if *v == max { nums.push(*k) } if *v > max { max = *v; nums.clear(); nums.push(*k) } } nums.sort(); nums}