Given an array of integers, return a new array containing only the unique values.
The resulting array can be in any order.
Example:
Input: [2, 3, 1, 1, 4, 3, -2, 1] Output: [2, 3, 1, 4, -2]
For this lesson, your algorithm should run in O(n^2) time and use O(n) extra space.
(There are faster solutions which we will discuss in future lessons)
The task is to remove duplicates from an array of integers and return a new array containing only the unique values. The resulting array can be in any order.
[2, 3, 1, 1, 4, 3, -2, 1]
[2, 3, 1, 4, -2]
Input: [2, 3, 1, 1, 4, 3, -2, 1] Output: [2, 3, 1, 4, -2]
The core challenge is to identify and remove duplicate values from the array while maintaining the order of first occurrences. This problem is significant in various applications such as data cleaning, where duplicate entries need to be removed.
Potential pitfalls include not handling negative numbers or assuming the array is sorted.
To solve this problem, we can use an extra array to store unique values. We will traverse the input array and add each element to the new array only if it has not been added before.
A naive solution would involve checking each element against all previously added elements, leading to an O(n^2) time complexity. This is not optimal but meets the problem's constraints.
While the naive solution is acceptable for this lesson, a more optimized approach would involve using a set to track seen elements, reducing the time complexity to O(n). However, this is beyond the scope of this lesson.
Here is a step-by-step breakdown of the naive algorithm:
uniqueVals
to store unique values.uniqueVals
.uniqueVals
.uniqueVals
as the result.def remove_duplicates(nums):
# Initialize an empty list to store unique values
unique_vals = []
# Iterate through each element in the input array
for num in nums:
# Check if the element is already in unique_vals
if num not in unique_vals:
# If not, add it to unique_vals
unique_vals.append(num)
# Return the list of unique values
return unique_vals
# Example usage
input_array = [2, 3, 1, 1, 4, 3, -2, 1]
print(remove_duplicates(input_array)) # Output: [2, 3, 1, 4, -2]
The time complexity of this approach is O(n^2) because for each element, we are checking its presence in the unique_vals
list, which takes O(n) time in the worst case. The space complexity is O(n) as we are using an extra list to store unique values.
Potential edge cases include:
Input: [] Output: [] Input: [1, 1, 1, 1] Output: [1] Input: [-1, -1, -2, -2] Output: [-1, -2]
To test the solution comprehensively, consider the following test cases:
def test_remove_duplicates():
assert remove_duplicates([2, 3, 1, 1, 4, 3, -2, 1]) == [2, 3, 1, 4, -2]
assert remove_duplicates([]) == []
assert remove_duplicates([1, 1, 1, 1]) == [1]
assert remove_duplicates([-1, -1, -2, -2]) == [-1, -2]
assert remove_duplicates([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]
test_remove_duplicates()
When approaching such problems, consider the following tips:
In this blog post, we discussed how to remove duplicates from an array in O(n^2) time complexity using Python. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills.
For further reading and practice, consider the following resources: