CJCoding With Joseph
15free / daySign inCreate account

Remove Duplicates Keeping Order

Write 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 — print one after the last line.

Hints4 available
Topics:
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.