1000. Remove Duplicates from Unsorted ArrayPOTD
You are given an unsorted array arr[] of integers that may contain duplicates. Your task is to remove the duplicate elements, keeping only the first occurrence of each element, and return the resulting array.
Example 1:
Input: arr = [1, 2, 3, 1, 4, 2]
Output: [1, 2, 3, 4]
Explanation:
1 and 2 are repeated; only their first occurrence is kept.
Example 2:
Input: arr = [1, 2, 3, 4]
Output: [1, 2, 3, 4]
Explanation:
No duplicate elements found.
Now Your Turn!
Pick the correct output for the given inputInput: arr = [4, 5, 4, 2, 5, 1, 2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= arr.length <= 10⁶
- 1 <= arr[i] <= 10⁵