Fácil 0 resueltos 0% de aceptación

Remove Duplicates

Keep only the first occurrence of each value.

Lo que aprenderás

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

Problema

Ver traducción

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.

Restricciones

0 ≤ length ≤ 100,000

Ejemplos

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

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

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

Salida esperada:
long[] expected = new long[] {3, 1, 2};

Explicación

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

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

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

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

Salida esperada:
long[] expected = new long[] {7};

Explicación

Only the first 7 stays: [7].

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

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

Pista 2

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

Pista 3

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

Complejidad esperada

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

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.