Skip to content

Commit

Permalink
feat: median of list
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Klingenberg <[email protected]>
  • Loading branch information
fredrkl committed Mar 29, 2024
1 parent 87a7770 commit 234e082
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions projects/hash-map/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,27 @@ fn main() {
scores.entry(String::from("Red")).or_insert(75);

println!("{:?}", scores);

let text = "hello world wonderful world";
let mut map = HashMap::new();

for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);

let mut _numberlist = [9, 3, 1, 4, 2, 6, 5, 7, 8, 0];
_numberlist.sort();

if _numberlist.len() % 2 == 0 {
let _mid = _numberlist.len() / 2;
let _median = (_numberlist[_mid] + _numberlist[_mid - 1]) / 2;
println!("Median: {}", _median);
} else {
let _mid = _numberlist.len() / 2;
let _median = _numberlist[_mid];
println!("Median: {}", _median);
}
println!("{:?}", _numberlist);
}

0 comments on commit 234e082

Please sign in to comment.