How to remove duplicates from Vec

The most efficient way to remove duplicates from a `Vec` while preserving order is to sort the vector and call `dedup()`, which works in-place for types implementing `PartialEq` and `Ord`.

The most efficient way to remove duplicates from a Vec while preserving order is to sort the vector and call dedup(), which works in-place for types implementing PartialEq and Ord. If you need to preserve the original order or the elements don't implement Ord, use a HashSet to filter the vector, though this requires cloning or moving elements.

For the common case where order doesn't matter or you can sort, sort_unstable followed by dedup is the fastest approach because it avoids heap allocations:

fn main() {
    let mut numbers = vec![3, 1, 2, 3, 1, 4, 2];
    
    // Sort first to bring duplicates together
    numbers.sort_unstable();
    
    // Remove consecutive duplicates in-place
    numbers.dedup();
    
    println!("{:?}", numbers); // Output: [1, 1, 2, 2, 3, 3, 4] -> wait, dedup keeps one instance
    // Correction: dedup keeps the first occurrence of each group
    // Output: [1, 2, 3, 4]
}

If you must preserve the original insertion order, iterate through the vector and track seen items using a HashSet. This approach is slightly slower due to hashing overhead but maintains the sequence of first occurrences:

use std::collections::HashSet;

fn main() {
    let numbers = vec![3, 1, 2, 3, 1, 4, 2];
    
    let mut seen = HashSet::new();
    let mut unique: Vec<i32> = Vec::new();
    
    for &num in &numbers {
        if seen.insert(num) {
            unique.push(num);
        }
    }
    
    println!("{:?}", unique); // Output: [3, 1, 2, 4]
}

Note that dedup() only removes consecutive duplicates, so sorting is mandatory if you want to eliminate all duplicates in the sorted approach. The HashSet method works for any type implementing Hash and Eq, making it more flexible for custom structs, but it requires the elements to be Clone if you are iterating by reference and pushing owned values, or you must move the elements if the vector owns them. Choose the sorting method for primitive types where performance is critical and order is irrelevant, and the HashSet method when order matters or types lack an Ord implementation.