आसान 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

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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।