Let's solve an easy-level array problem.
We have a sorted array arr
and we need to remove the duplicate elements such that each element appears only once.
Examples:
So, here in these examples, we can see the size of the output array is the same as the input array. We need to keep the unique elements in the same order as they appear in the input array i.e., in sorted order.
We don't need to deallocate the old space and create a new array of smaller sizes and have unique elements.
We just need to ensure that the unique elements of the input array come at front positions and we return the size of the same. We don't need to take care of elements that come after the unique elements.
We can also do this to ensure it works in all languages since it is impossible to change the length of the array in some languages. Hence, the array has space for the previous number of elements but we return a new size variable to determine how many elements are to be considered.
So, now let's write a function that:
takes an array as an argument,
shifts the unique elements to the beginning of the array,
returns the size of the modified array(number of unique elements).
Method 1(Basic):
Approach:
Create a temporary array of the same size as the input array,
Copy only unique elements to the temp array,
Maintain a variable to store the size of the temp array.
This approach takes O(n) time and O(n) extra space.
Let's see the implementation:
class Solution {
public int removeDuplicates(int[] arr) {
int n = arr.length;
int temp[] = new int[n]; // A temporary array of size n
temp[0] = arr[0]; // Initializing 1st element to index 0 of temp array, as it will always come in the resulting aray.
int size = 1; // A new variable to track size
// Let's copy the unique elements to temp array.
for(int i = 1; i < n; i++)
{
if(temp[size - 1] != arr[i])
{
temp[size] = arr[i];
size++; // increment size of temp if condition is true
}
}
// Copy back the unique values to the beginning of original array.
for(int i = 0; i < size; i++)
{
arr[i] = temp[i];
}
return size; // the no. of distinct values
}
}
Let's see the working of the above code:
Method 2(Efficient):
In this method, we do not need to create a temporary array which will result in saving extra space needed.
Approach:
Define a size variable and initialize it as 1 as we will start our loop from the 2nd element.
Run a loop to check if the current element is distinct or not.
If it not unique, ignore it.
If yes, then it is a unique element hence, copy it to the array[size] position.
This approach requires O(n) time and O(1) space i.e., constant space. Hence, efficient!
Let's see the implementation:
class Solution {
public int removeDuplicates(int[] arr) {
int n = arr.length;
int size = 1; // initialize a size variable
for(int i = 1 ; i < n; i++){
// check if current element is unique or not?
if(arr[i] != arr[size-1]){
arr[size] = arr[i]; // if yes, copy at arr[size] pos.
size++; // increment the size of modified array by 1 since it is a unique element
}
}
return size; // the number of unique elements
}
}
Let's see the working of the above code:
So, here we end the problem!
You can solve this problem on Leetcode. The link is embedded below.
Conclusion:
Thanks ๐ for reading and following this blog till the end! I appreciate your time and effort. I hope your concept for this problem is clear and keep practising problems!
If you have any suggestions/questions, feel free to connect with me on Twitter and LinkedIn.
And if you feel this article helped you, don't forget to give it a thumbs up and share it with your friends and do follow me on Twitter.
See you all next time! ๐
Regards,
Vaibhav