
Problem
Solution
Submissions
Remove Duplicates from an Array
Certification: Basic Level
Accuracy: 50%
Submissions: 6
Points: 10
Write a C++ function to remove duplicates from an array of integers and return the new array.
Example 1
- Input: arr = [1, 2, 2, 3, 4, 4]
- Output: [1, 2, 3, 4]
- Explanation:
- Step 1: Create an empty result array and a hash set to track seen elements.
- Step 2: Iterate through the input array: [1, 2, 2, 3, 4, 4].
- Step 3: For each element, check if it's already in the hash set.
- Step 4: If not in the set, add it to both the set and the result array.
- Step 5: Return the result array containing unique elements: [1, 2, 3, 4].
Example 2
- Input: arr = [5, 5, 5, 5]
- Output: [5]
- Explanation:
- Step 1: Create an empty result array and a hash set to track seen elements.
- Step 2: Iterate through the input array: [5, 5, 5, 5].
- Step 3: For each element, check if it's already in the hash set.
- Step 4: If not in the set, add it to both the set and the result array.
- Step 5: Return the result array containing unique elements: [5].
Constraints
- 1 ≤ array length ≤ 10^4
- -10^5 ≤ array elements ≤ 10^5
- Time Complexity: O(n)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a hash set to store unique elements.
- Traverse the array and add elements to the set if they are not already present.
- Convert the set back to an array and return it.