問題
Return the list with repeated values removed, keeping the first occurrence of each value in its original order.
Implement remove_duplicates(numbers).
Input
numbers: The list of integers.
Output
Return the list without duplicates.
制約
0 ≤ length ≤ 100,000
例
入力:
numbers = [3, 1, 3, 2, 1]
期待される出力:
expected = [3, 1, 2]
入力:
std::vector<long long> numbers = {3, 1, 3, 2, 1};
期待される出力:
std::vector<long long> expected = {3, 1, 2};
入力:
long[] numbers = new long[] {3, 1, 3, 2, 1};
期待される出力:
long[] expected = new long[] {3, 1, 2};
解説
Keep 3, 1, then 2; the later 3 and 1 are dropped: [3, 1, 2].
入力:
numbers = [7, 7, 7]
期待される出力:
expected = [7]
入力:
std::vector<long long> numbers = {7, 7, 7};
期待される出力:
std::vector<long long> expected = {7};
入力:
long[] numbers = new long[] {7, 7, 7};
期待される出力:
long[] expected = new long[] {7};
解説
Only the first 7 stays: [7].
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Keep the first time each value appears and ignore later repeats.
ヒント 2
A set or similar lookup helps you decide quickly whether a value was already seen.
ヒント 3
Preserve the original order of the first occurrences, not sorted order.
目安の計算量
O(n) time, O(n) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。