← Back to STL Containers
Question 571
Remove Duplicates Keeping Order
Remove Duplicates Keeping Order
Write a functionvoid dedupe(vector<int>& v)that modifiesvin place so that each value appears only once, keeping the FIRST occurrence of every value and preserving the original relative order. Walkvwith a single iterator; keep aset<int>of values you have already kept. If the current value is already in the set, erase it withit = v.erase(it); otherwise insert it into the set and++it. Do not sort and do not build a second vector. The test prints the remaining elements separated by spaces on one line. For {3, 1, 3, 2, 1} the line is3 1 2; for {1, 1, 1} it is1. Do NOT write a main function.
Expected Output:
3 1 2 1
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersIterators
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.