आसान 0 हल किए गए 0% स्वीकृति

Remove Duplicates

Keep only the first occurrence of each value.

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

0 ≤ length ≤ 100,000

उदाहरण

उदाहरण 1
इनपुट:
numbers = [3, 1, 3, 2, 1]

अपेक्षित आउटपुट:
expected = [3, 1, 2]
इनपुट:
std::vector<long long> numbers = {3, 1, 3, 2, 1};

अपेक्षित आउटपुट:
std::vector<long long> expected = {3, 1, 2};
इनपुट:
long[] numbers = new long[] {3, 1, 3, 2, 1};

अपेक्षित आउटपुट:
long[] expected = new long[] {3, 1, 2};

व्याख्या

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

उदाहरण 2
इनपुट:
numbers = [7, 7, 7]

अपेक्षित आउटपुट:
expected = [7]
इनपुट:
std::vector<long long> numbers = {7, 7, 7};

अपेक्षित आउटपुट:
std::vector<long long> expected = {7};
इनपुट:
long[] numbers = new long[] {7, 7, 7};

अपेक्षित आउटपुट:
long[] expected = new long[] {7};

व्याख्या

Only the first 7 stays: [7].

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

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

संकेत 2

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

संकेत 3

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

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

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

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

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