Python Basics簡単

Removing Duplicate elements from an array

Detailed guide and Python implementation for the 'Removing Duplicate elements from an array' problem.

問題提起

簡単

Write a function remove_duplicates(arr) that takes a list of integers arr and returns a new list with duplicate elements removed, preserving the order of first occurrence of each element.

制約
  • 0 <= len(arr) <= 10^5
  • -10^6 <= arr[i] <= 10^6

Example 1
Input
arr = [1, 2, 2, 3, 4, 4, 5]
Output
[1, 2, 3, 4, 5]
Explanation

Remove the second 2 and second 4, keeping first occurrences in order.

Example 2
Input
arr = [5, 5, 5, 5]
Output
[5]
Explanation

All duplicates removed, keeping only one 5.

Example 3
Input
arr = [1, 2, 3]
Output
[1, 2, 3]
Explanation

No duplicates, array unchanged.

Need a Hint?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。