簡単 0件解決済み 正答率0%

Remove Duplicates

Keep only the first occurrence of each value.

このチャレンジで学べること

  • Track seen values with a set
  • Keep the first occurrence of each value
  • Preserve the original order

問題

翻訳を表示

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

例

例 1
入力:
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].

例 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。