28. Remove Duplicates in Array
The problem can be found at the following link: Question Link
Problem Description
Given an array arr consisting of positive integer numbers, remove all duplicate numbers. The modified array should contain only unique elements.
Example:
Input:
arr[] = [2, 2, 3, 3, 7, 5]Output:
[2, 3, 7, 5]Explanation: After removing the duplicates 2 and 3, we get the array [2, 3, 7, 5].
Input:
arr[] = [2, 2, 5, 5, 7, 7]Output:
[2, 5, 7]Input:
arr[] = [8, 7]Output:
[8, 7]Constraints:
1 ≤ arr.size() ≤ 10^6
2 ≤ arr[i] ≤ 100
My Approach
Using a Set for Uniqueness:
Utilize a set to keep track of the unique elements encountered while iterating through the array. Sets automatically handle duplicates for us.
Building the Result:
Iterate through the array, adding elements to the set. If an element is already present in the set, it is ignored.
Finally, convert the set back to a vector or array for the result.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the length of the input array. Each element is processed once.
Expected Auxiliary Space Complexity: O(n), as we use a set to store the unique elements.
Code (C++)
Code (Java)
Code (Python)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit my LinkedIn: Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
⭐ Star this repository if you find it helpful or intriguing! ⭐
📍Visitor Count
Last updated