Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Remove Duplicates

Keep only the first occurrence of each value.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ length ≤ 100,000

Ví dụ

Ví dụ 1
Đầu vào:
numbers = [3, 1, 3, 2, 1]

Kết quả mong đợi:
expected = [3, 1, 2]
Đầu vào:
std::vector<long long> numbers = {3, 1, 3, 2, 1};

Kết quả mong đợi:
std::vector<long long> expected = {3, 1, 2};
Đầu vào:
long[] numbers = new long[] {3, 1, 3, 2, 1};

Kết quả mong đợi:
long[] expected = new long[] {3, 1, 2};

Giải thích

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

Ví dụ 2
Đầu vào:
numbers = [7, 7, 7]

Kết quả mong đợi:
expected = [7]
Đầu vào:
std::vector<long long> numbers = {7, 7, 7};

Kết quả mong đợi:
std::vector<long long> expected = {7};
Đầu vào:
long[] numbers = new long[] {7, 7, 7};

Kết quả mong đợi:
long[] expected = new long[] {7};

Giải thích

Only the first 7 stays: [7].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.