Tutte le sfide
Facile 0 risolte 0% di accettazione

Unique Records

Remove duplicate records that are equal by value, keeping the first of each in order.

Cosa imparerai

  • Compare records by their values
  • Remove duplicates while keeping order
  • Normalise fields by trimming spaces

Problema

Mostra traduzione

C# records compare by value: two records are equal when all their fields are equal, even if they are different objects. That makes removing duplicates easy.

Each record is written name,city. Trim spaces around both fields; after trimming, two records are equal when both fields match exactly (case matters).

Implement unique_records(records).

Input

records: The records, in order.

Output

Return the distinct records in the order they first appear, written name,city with the trimmed fields.

Vincoli

0 ≤ number of records ≤ 100,000

Each field has 1 to 30 characters and no commas

Esempi

Esempio 1
Input:
records = ["Ada,London", "Lin, Hanoi", "Ada , London"]

Output atteso:
expected = ["Ada,London", "Lin,Hanoi"]
Input:
std::vector<std::string> records = {"Ada,London", "Lin, Hanoi", "Ada , London"};

Output atteso:
std::vector<std::string> expected = {"Ada,London", "Lin,Hanoi"};
Input:
string[] records = new string[] {"Ada,London", "Lin, Hanoi", "Ada , London"};

Output atteso:
string[] expected = new string[] {"Ada,London", "Lin,Hanoi"};

Spiegazione

After trimming, the third record equals the first, so it is dropped: ["Ada,London", "Lin,Hanoi"].

Esempio 2
Input:
records = ["ada,Paris", "Ada,Paris"]

Output atteso:
expected = ["ada,Paris", "Ada,Paris"]
Input:
std::vector<std::string> records = {"ada,Paris", "Ada,Paris"};

Output atteso:
std::vector<std::string> expected = {"ada,Paris", "Ada,Paris"};
Input:
string[] records = new string[] {"ada,Paris", "Ada,Paris"};

Output atteso:
string[] expected = new string[] {"ada,Paris", "Ada,Paris"};

Spiegazione

Case matters, so these two records are different: ["ada,Paris", "Ada,Paris"].

Come affrontarlo

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

Suggerimento 1

Trim both fields before comparing records.

Suggerimento 2

A set of (name, city) pairs remembers which records you have seen.

Suggerimento 3

Output each new record the first time it appears, in input order.

Complessità attesa

O(total length) time, O(number of records) 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.