Remove Duplicates Keeping Order
hardQuestion 571Write a function void dedupe(vector<int>& v) that modifies v in place so that each value appears only once, keeping the FIRST occurrence of every value and preserving the original relative order. Walk v with a single iterator; keep a set<int> of values you have already kept. If the current value is already in the set, erase it with it = 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 is 3 1 2; for {1, 1, 1} it is 1.
Do NOT write a main function.
Expected output
3 1 2 1
= newline. The output ends with a newline, so 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.