Fácil 0 resolvidos 0% de aceitação

Remove Duplicates

Keep only the first occurrence of each value.

O que você vai aprender

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

Problema

Ver tradução

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.

Restrições

0 ≤ length ≤ 100,000

Exemplos

Exemplo 1
Entrada:
numbers = [3, 1, 3, 2, 1]

Saída esperada:
expected = [3, 1, 2]
Entrada:
std::vector<long long> numbers = {3, 1, 3, 2, 1};

Saída esperada:
std::vector<long long> expected = {3, 1, 2};
Entrada:
long[] numbers = new long[] {3, 1, 3, 2, 1};

Saída esperada:
long[] expected = new long[] {3, 1, 2};

Explicação

Keep 3, 1, then 2; the later 3 and 1 are dropped: [3, 1, 2].

Exemplo 2
Entrada:
numbers = [7, 7, 7]

Saída esperada:
expected = [7]
Entrada:
std::vector<long long> numbers = {7, 7, 7};

Saída esperada:
std::vector<long long> expected = {7};
Entrada:
long[] numbers = new long[] {7, 7, 7};

Saída esperada:
long[] expected = new long[] {7};

Explicação

Only the first 7 stays: [7].

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Keep the first time each value appears and ignore later repeats.

Dica 2

A set or similar lookup helps you decide quickly whether a value was already seen.

Dica 3

Preserve the original order of the first occurrences, not sorted order.

Complexidade esperada

O(n) time, O(n) extra space

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.