Snippets Collections
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    // Vector of integers
    std::vector<int> vecObj {11, 12, 13, 14, 15, 16, 12, 43, 12, 13, 11};

    // Sort the values in vector
    std::sort(vecObj.begin(), vecObj.end());

    // Remove duplicate values from vector
    vecObj.erase(std::unique(vecObj.begin(), vecObj.end()), vecObj.end());

    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }

    return 0;
}
star

Thu May 25 2023 17:34:22 GMT+0000 (Coordinated Universal Time) https://devptr.com/how-to-remove-duplicates-from-a-vector-in-c/

#c++ #remove_duplicates #vector

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension