Unique function in Rust

how to remove the duplicate values from a vectors of type f64, please help me out.
How to retrieve the unique elements from the vector of floats using unique and unique by

You can put them into a HashSet.

let unique_floats: HashSet<_> = floats.iter().copied().collect();

You can also sort and use dedup.

// floats is a vector
floats.sort_by(|a, b| a.partial_cmp(b).expect("NaN in vector"));

If you have a vector that is already sorted, you can use Vec::dedup, Vec::dedup_by, or Vec::dedup_by_key.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.