Frequency of unique elements

I am trying to print a vector of tuple that will contain the element and its frequency:

pub fn getting_uniques(column: &mut Vec<String>) -> Vec<String> {
    column.dedup();
    column.to_vec()
}

fn get_frequency(vec: &Vec<String>, element: String) -> usize {
    vec.iter().filter(|&n| *n == element).count() // returns the frequency of an element in the vector
}

pub fn most_common_text(column: &mut Vec<String>){
    let uniques = getting_uniques(column);
    let duplicate_uniques = uniques.clone();
    let element_frequency : Vec<(&String, usize)>  = duplicate_uniques.into_iter()
     .map(|&x| (*x, get_frequency(&uniques, *x)))
     .collect();
 println!("{:?}", element_frequency);
}

This is the error I am getting:

let element_frequency : Vec<(&String, usize)> = duplicate_uniques.into_iter().map(|&x| (*x, get_frequency(&uniques, *x))).collect();
| ^--
| ||
| |expected due to this
| expected struct std::string::String, found reference
| help: did you mean x: &std::string::String
|

Can someone help me out!

Please read the pinned post about code formatting and edit your post accordingly.

Yeah, I did !

element: String is an owned type you would only take as an argument if you have to store the element permanently into some collection during that function call.

For a temporary access to reading/comparing element you should use borrowed type like &str (generally &String converts automatically to &str).

BTW: your algorithm is quadratic, which makes it extremely slow, even in a fast language. You should use a HashMap to count frequencies, not nested loops.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.