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.
An array of integers, e.g., [2, 3, 1, 1, 4, 3, -2, 1]
A new array containing only the unique values, e.g., [2, 3, 1, 4, -2]
The core challenge is to identify and remove duplicate values from the array. This problem is significant in various applications such as data cleaning, where duplicate entries need to be removed to ensure data integrity.
Potential pitfalls include not handling negative numbers or assuming the input 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 seen before.
A naive solution would involve nested loops to check for duplicates, resulting in O(n^3) time complexity, which is not optimal.
We can improve this by using an extra array to store unique values and checking for duplicates using a linear search, resulting in O(n^2) time complexity.
Here is a step-by-step breakdown of the algorithm:
uniqueVals
to store unique values.nums
from left to right.nums
, check if it is already in uniqueVals
.uniqueVals
, add it to uniqueVals
.uniqueVals
array.import java.util.ArrayList;
import java.util.List;
public class RemoveDuplicates {
public static int[] removeDuplicates(int[] nums) {
// Initialize an empty list to store unique values
List<Integer> uniqueVals = new ArrayList<>();
// Traverse the input array
for (int i = 0; i < nums.length; i++) {
// Check if the current element is already in uniqueVals
if (!uniqueVals.contains(nums[i])) {
// If not, add it to uniqueVals
uniqueVals.add(nums[i]);
}
}
// Convert the list to an array and return
int[] result = new int[uniqueVals.size()];
for (int i = 0; i < uniqueVals.size(); i++) {
result[i] = uniqueVals.get(i);
}
return result;
}
public static void main(String[] args) {
int[] nums = {2, 3, 1, 1, 4, 3, -2, 1};
int[] uniqueNums = removeDuplicates(nums);
for (int num : uniqueNums) {
System.out.print(num + " ");
}
}
}
The time complexity of this approach is O(n^2) because for each element in the input array, we perform a linear search in the uniqueVals
array. The space complexity is O(n) because we use an extra array to store unique values.
Potential edge cases include:
Example:
Input: [] Output: []
Input: [1, 1, 1, 1] Output: [1]
To test the solution comprehensively, consider a variety of test cases:
When approaching such problems, it is essential to:
In this blog post, we discussed how to remove duplicates from an array in O(n^2) time complexity using Java. 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: