use std::{collections::HashMap, hash::Hash};
fn group_by_most_common_element<T: Hash + Eq + Copy>(
lists: Vec<Vec<T>>,
) -> HashMap<T, Vec<Vec<T>>> {
// A global count of each element that i can refer back to
let mut element_count = HashMap::new();
// The final result map
let mut result_map: HashMap<T, Vec<Vec<T>>> = HashMap::new();
for list in &lists {
for &element in list {
// count each occurance of each element contained in list.
*element_count.entry(element).or_insert(0) += 1;
}
}
// represent all elements in the result map
// even if they dont actually exist
for &element in element_count.keys() {
result_map.entry(element).or_default();
}
for list in lists {
if let Some(most_common_element) = list
.iter()
// Find the most common element in the list based on the global count
.max_by_key(|&element| element_count.get(element).unwrap_or(&0))
{
result_map
// create a new element in the result map with the found most common element
.entry(*most_common_element)
.or_default()
// pushes the list to the array if it exists, or it creates one then pushes it on too.
.push(list);
}
}
result_map
}
fn main() {
let list: Vec<Vec<&str>> = vec![
vec!["red", "green"],
vec!["red", "blue"],
vec!["red"],
vec!["green", "blue"],
vec!["blue", "yellow"],
vec!["yellow"],
vec!["yellow"],
vec!["yellow", "red"],
vec!["red"],
vec!["green", "red"],
vec!["red", "green"],
];
let map = group_by_most_common_element(list);
println!("{:?}", map);
}