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)
uniqueVals
and push the unique values from nums
there.
We can traverse the array nums
from left to right using an index i
. We should push nums[i]
into uniqueVals
only if that value has not been seen before. How can we check this?
nums[i]
has never been seen before if it doesn't occur in uniqueVals
.
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.
Array of integers, e.g., [2, 3, 1, 1, 4, 3, -2, 1]
Array of unique integers, e.g., [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. 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 zero correctly, and misunderstanding the requirement to maintain the order of first occurrences.
To solve this problem, we can use an extra array to store unique values. We will traverse the input array and for each element, check if it has already been added to the unique array. If not, we add it.
The naive solution involves nested loops to check for duplicates, resulting in O(n^2) time complexity. This is not optimal but meets the problem constraints.
We can use a Set to keep track of seen values, reducing the need for nested loops. However, this would improve the time complexity to O(n), which is beyond the scope of this lesson.
Here is a step-by-step breakdown of the naive algorithm:
uniqueVals
.uniqueVals
.uniqueVals
.uniqueVals
as the result.// Function to remove duplicates from an array
function removeDuplicates(nums) {
// Initialize an empty array to store unique values
let uniqueVals = [];
// Iterate through each element in the input array
for (let i = 0; i < nums.length; i++) {
// Check if the current element is already in uniqueVals
if (!uniqueVals.includes(nums[i])) {
// If not, add it to uniqueVals
uniqueVals.push(nums[i]);
}
}
// Return the array of unique values
return uniqueVals;
}
// Example usage
const inputArray = [2, 3, 1, 1, 4, 3, -2, 1];
const result = removeDuplicates(inputArray);
console.log(result); // Output: [2, 3, 1, 4, -2]
The time complexity of this approach is O(n^2) because for each element, we are checking if it exists in the uniqueVals
array, which takes O(n) time. The space complexity is O(n) as we are using an extra array to store unique values.
Potential edge cases include:
Examples:
Input: [] Output: [] Input: [1, 1, 1] Output: [1] Input: [-1, 0, -1, 0] Output: [-1, 0]
To test the solution comprehensively, consider the following test cases:
Using a testing framework like Jest can help automate and manage these tests.
When approaching such problems:
In this lesson, we discussed how to remove duplicates from an array using a naive approach with O(n^2) time complexity. Understanding and solving such problems is crucial for developing strong problem-solving skills. Practice and exploration of optimized solutions are encouraged.
For further reading and practice: