Einfach 0 gelöst 0% Erfolgsquote

Remove Duplicates

Keep only the first occurrence of each value.

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ length ≤ 100,000

Beispiele

Beispiel 1
Eingabe:
numbers = [3, 1, 3, 2, 1]

Erwartete Ausgabe:
expected = [3, 1, 2]
Eingabe:
std::vector<long long> numbers = {3, 1, 3, 2, 1};

Erwartete Ausgabe:
std::vector<long long> expected = {3, 1, 2};
Eingabe:
long[] numbers = new long[] {3, 1, 3, 2, 1};

Erwartete Ausgabe:
long[] expected = new long[] {3, 1, 2};

Erläuterung

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

Beispiel 2
Eingabe:
numbers = [7, 7, 7]

Erwartete Ausgabe:
expected = [7]
Eingabe:
std::vector<long long> numbers = {7, 7, 7};

Erwartete Ausgabe:
std::vector<long long> expected = {7};
Eingabe:
long[] numbers = new long[] {7, 7, 7};

Erwartete Ausgabe:
long[] expected = new long[] {7};

Erläuterung

Only the first 7 stays: [7].

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.