簡単 0件解決済み 正答率0%

Unique Records

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

0 ≤ number of records ≤ 100,000

Each field has 1 to 30 characters and no commas

例

例 1
入力:
records = ["Ada,London", "Lin, Hanoi", "Ada , London"]

期待される出力:
expected = ["Ada,London", "Lin,Hanoi"]
入力:
std::vector<std::string> records = {"Ada,London", "Lin, Hanoi", "Ada , London"};

期待される出力:
std::vector<std::string> expected = {"Ada,London", "Lin,Hanoi"};
入力:
string[] records = new string[] {"Ada,London", "Lin, Hanoi", "Ada , London"};

期待される出力:
string[] expected = new string[] {"Ada,London", "Lin,Hanoi"};

解説

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

例 2
入力:
records = ["ada,Paris", "Ada,Paris"]

期待される出力:
expected = ["ada,Paris", "Ada,Paris"]
入力:
std::vector<std::string> records = {"ada,Paris", "Ada,Paris"};

期待される出力:
std::vector<std::string> expected = {"ada,Paris", "Ada,Paris"};
入力:
string[] records = new string[] {"ada,Paris", "Ada,Paris"};

期待される出力:
string[] expected = new string[] {"ada,Paris", "Ada,Paris"};

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Trim both fields before comparing records.

ヒント 2

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

ヒント 3

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

目安の計算量

O(total length) time, O(number of records) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。