Torna alle sfide
Facile 0 risolte 0% di accettazione

Remove Duplicates

Keep only the first occurrence of each value.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

0 ≤ length ≤ 100,000

Esempi

Esempio 1
Input:
numbers = [3, 1, 3, 2, 1]

Output atteso:
expected = [3, 1, 2]
Input:
std::vector<long long> numbers = {3, 1, 3, 2, 1};

Output atteso:
std::vector<long long> expected = {3, 1, 2};
Input:
long[] numbers = new long[] {3, 1, 3, 2, 1};

Output atteso:
long[] expected = new long[] {3, 1, 2};

Spiegazione

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

Esempio 2
Input:
numbers = [7, 7, 7]

Output atteso:
expected = [7]
Input:
std::vector<long long> numbers = {7, 7, 7};

Output atteso:
std::vector<long long> expected = {7};
Input:
long[] numbers = new long[] {7, 7, 7};

Output atteso:
long[] expected = new long[] {7};

Spiegazione

Only the first 7 stays: [7].

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.