You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Count number of instances of each element. Then sort the counts in descending order. Now add them until you reach a sum that is larger than or equal to half of size of arr
impl Solution {
pub fn min_set_size(arr: Vec<i32>) -> i32 {
// Count instances of each element
let mut map = HashMap::new();
for element in &arr {
let count = map.entry(element).or_insert(0);
*count += 1;
}
// Sort the counts in descending order
let mut count_vec: Vec<_> = map.iter().collect();
count_vec.sort_by(|a, b| b.1.cmp(a.1));
// Add the count instances until the sum is larger than or equal to half the size of arr