Легко Решено: 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

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. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.