Đề bài
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ụ
Đầ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].
Đầ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.