Home
Map
Vec dedup ExampleUse the dedup and sort functions to remove duplicates from vectors. Act upon keys with dedup_by_key.
Rust
This page was last reviewed on Jun 7, 2023.
Remove duplicates. In Rust we find a dedup function on Vectors. And this can be used to eliminate duplicates from a Vector—but only a sorted Vector.
Shows a set
Usage notes. When using dedup(), we must either have kept the vector in sorted order, or we need to sort it before calling dedup. Otherwise dedup and dedup_by_key will not work.
First example. Consider this string vector: it has 3 animal names, and they are not sorted. We must first sort the data collection before calling dedup.
String Array
Step 1 We call sort() with no argument to sort by value (alphabetically). The 2 birds are now at the start of the vector.
Step 2 Here we invoke dedup on the sorted list. We see that one of the 2 birds was removed, but the fish remains in the vector.
Tip Try removing the sort() call, and you will see that dedup does not work correctly without it.
Shows a set
fn main() { let mut animals = vec!["bird", "fish", "bird"]; println!("{:?}", animals); // Step 1: sort the vector. animals.sort(); println!("{:?}", animals); // Step 2: call dedup to remove duplicates in a sorted vector. animals.dedup(); println!("{:?}", animals); }
["bird", "fish", "bird"] ["bird", "bird", "fish"] ["bird", "fish"]
Key example. In this example, we use the dedup_by_key function, along with sort_by_key. These 2 methods transform elements into keys before acting upon the values.
Info We pass a closure to the sort_by_key and dedup_by_key functions. It is easier to pass the closure directly as an argument.
Detail To treat "cat" in the same way as the "CAT," we call to_uppercase() to act upon all elements as though they are uppercase.
fn main() { let mut animals = vec!["cat", "bird", "CAT"]; // First sort he vector with keys. // ... Then remove duplicates with keys. animals.sort_by_key(|a| a.to_uppercase()); animals.dedup_by_key(|a| a.to_uppercase()); println!("{:?}", animals); }
["bird", "cat"]
A summary. In Rust, using dedup() and dedup_by_key() is often a 2-step process. We must first sort the vector before invoking these functions—just adjacent elements are removed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.