Removing the duplicates from Vector

PHOTO EMBED

Mon Jun 10 2024 13:47:10 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

int main() {
    std::vector<int> vec = {1, 2, 2, 3, 4, 4, 5};

    // Sort the vector
    std::sort(vec.begin(), vec.end());

    // Use unique to remove duplicates
    auto last = std::unique(vec.begin(), vec.end());

    // Erase the unused elements
    vec.erase(last, vec.end());

    // Print the result
    for(int n : vec) {
        std::cout << n << " ";
    }

    return 0;
}
content_copyCOPY