---
title: "Write a function to remove duplicate elements from a vector in Rust."  
description: "Write a function to remove duplicate elements from a vector in Rust."  
author: "Utpal Vishwas"  
published: 2023-06-20  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158810/write-a-function-to-remove-duplicate-elements-from-a-vector-in-rust  
category: "rust"  
tags: ["rust"]  
reading_time: 2 minutes  

---

# Write a function to remove duplicate elements from a vector in Rust.

Write a [function to remove duplicate](https://www.mindstick.com/forum/158773/write-a-function-to-remove-duplicate-elements-from-a-list-in-python) [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) from a [vector](https://www.mindstick.com/forum/33499/vector-vs-arraylist-which-has-better-performance-and-why) in Rust.

## Replies

### Reply by Aryan Kumar

Sure, here is a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) [duplicate elements](https://www.mindstick.com/forum/158954/how-to-remove-duplicate-elements-from-a-list-in-python) from a vector in Rust:

Rust

```plaintext
fn remove_duplicates(vector: &mut Vec<i32>) {
    let mut seen_elements: HashSet<i32> = HashSet::new();
    let mut new_vector: Vec<i32> = Vec::new();
    for element in vector {
        if !seen_elements.contains(element) {
            seen_elements.insert(element);
            new_vector.push(element);
        }
    }
    vector.clear();
    vector.append(&mut new_vector);
}

fn main() {
    let mut vector = vec![1, 1, 2, 3, 3, 4, 5, 5, 6];
    remove_duplicates(&mut vector);
    println!("The vector after removing duplicates is: {:?}", vector);
}
```

This function first defines a function called remove_duplicates that takes a mutable reference to a Vec of i32 as input. The function first initializes a HashSet called seen_elements to store all the elements that have been seen. Then, the function creates a new vector called new_vector to store the elements that have not been seen. The function then iterates through the vector vector, checking if each element is in the seen_elements HashSet. If the element is not in the seen_elements HashSet, then the function adds the element to the new_vector vector and to the seen_elements HashSet. Finally, the function clears the vector vector and appends the new_vector vector to the vector vector.

The main function of the program then creates a vector called vector with the values 1, 1, 2, 3, 3, 4, 5, 5, 6. The function then calls the remove_duplicates function with the vector vector as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called remove_duplicates.rs and then compile it using the following command:

Code snippet

```plaintext
rustc remove_duplicates.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./remove_duplicates
```

This will print the following output to the console:

Code snippet

```plaintext
The vector after removing duplicates is: [1, 2, 3, 4, 5, 6]
```


---

Original Source: https://www.mindstick.com/forum/158810/write-a-function-to-remove-duplicate-elements-from-a-vector-in-rust

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
