How do I remove duplicates with std::list?

In C++, you can remove duplicates from a `std::list` using the `unique` function along with `sort`. The `unique` function removes consecutive duplicate elements, so before using it, you need to sort the list to group duplicates together.

// Example of removing duplicates from std::list in C++
#include <iostream>
#include <list>
#include <algorithm>

int main() {
    std::list<int> myList = {4, 1, 2, 2, 3, 4, 4, 1};
    
    // Step 1: Sort the list
    myList.sort();
    
    // Step 2: Remove duplicates
    myList.unique();
    
    // Display the result
    for (const int &value : myList) {
        std::cout << value << " ";
    }
    return 0;
}
    

C++ std::list remove duplicates sort unique algorithms