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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter