Dedupe preserving order in Rust
Removes duplicates while keeping the original order, which sorting would destroy.
fn dedupe(items: Vec<i32>) -> Vec<i32> {
let mut seen = HashSet::new();
items
.into_iter()
.filter(|item| seen.insert(*item))
.collect()
}
Keywords and builtins used here
Vecdedupefni32letmut
The run, in numbers
- Lines
- 7
- Characters to type
- 138
- Tokens
- 50
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 30 seconds.
Step 3 of 3 in Terminal operations, step 11 of 14 in Iterators & closures.