Retour aux défis
Facile 0 résolus 0 % de réussite

Remove Duplicates

Keep only the first occurrence of each value.

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

0 ≤ length ≤ 100,000

Exemples

Exemple 1
Entrée :
numbers = [3, 1, 3, 2, 1]

Sortie attendue :
expected = [3, 1, 2]
Entrée :
std::vector<long long> numbers = {3, 1, 3, 2, 1};

Sortie attendue :
std::vector<long long> expected = {3, 1, 2};
Entrée :
long[] numbers = new long[] {3, 1, 3, 2, 1};

Sortie attendue :
long[] expected = new long[] {3, 1, 2};

Explication

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

Exemple 2
Entrée :
numbers = [7, 7, 7]

Sortie attendue :
expected = [7]
Entrée :
std::vector<long long> numbers = {7, 7, 7};

Sortie attendue :
std::vector<long long> expected = {7};
Entrée :
long[] numbers = new long[] {7, 7, 7};

Sortie attendue :
long[] expected = new long[] {7};

Explication

Only the first 7 stays: [7].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.