I've parsed a file, split the string by lines and want to leave only unique elements in each vector. I expect vec.dedup()
to work like this:
let mut vec = vec!["a", "b", "a"];
vec.dedup();
assert_eq!(vec, ["a", "b"]);
But it fails:
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `["a", "b", "a"]`,
right: `["a", "b"]`', src/main.rs:4:4
How can I remove duplicates?
As documented,
Vec#dedup
only removes consecutive elements from a vector (it is much cheaper than a full deduplication). It would work fine if the vector wasvec!["a", "a", "b"]
, for example.Of course, there are multiple potential solutions.
In order to obtain a vector with all duplicates removed while retaining the original order of the elements, the
itertools
crate provides aunique
adaptor.If element order is not important, you may sort the elements first and then call dedupe.
If fast element lookup is important, you may also consider using a set type instead, such as
HashSet
.The other answer points out that a
HashSet
is a better choice for a collection without duplicates, which I agree with. This shows how to directly deduplicate aVec
using that property ofHashMap
and without sorting theVec
first to usestd::vec::Vec::dedup
.This is a fast (O(n)) solution, but creating the
HashSet
requires some extra memory.