Introduction
Hey,
Two weeks ago, I began solving daily LeetCode problems to develop my coding and problem-solving skills. I chose JavaScript, my most familiar language, for these challenges.
Today, I solved the problem 26: Remove Duplicates from Sorted Array.
Two weeks ago, I began solving daily LeetCode problems to develop my coding and problem-solving skills. I chose JavaScript, my most familiar language, for these challenges.
Today, I solved the problem 26: Remove Duplicates from Sorted Array.
Problem
Given a sorted array of numbers, the goal is to remove all duplicated numbers form the array so that in the end the array doesn`t includes any duplicates.
For example if you enter the array: nums=[1,1,2] the final output should be [1,2].
For example if you enter the array: nums=[1,1,2] the final output should be [1,2].
Solution
1) Structure
First of all we have given a fuction “removeDuplicates” with an array “nums” as parameter.
var removeDuplicates = function(nums{
}
2) Basic idea
My approach to solving this problem involves iterating over the array and comparing each value of nums[i] to the subsequent value of nums[i+1]. Then, I use an if-else statement to compare these two values, and if both are equal, I remove the value of nums[i].
3) First implementation
var removeDuplicates = function(nums{
for(let i=0; i<nums.length; i++){
if(nums[i]===nums[i+1]){
delete nums[i];
};
};
However, this approach led to a problem where it only removed the value, leaving an empty space in the array. Consequently, the output appeared as [,1,2], which is incorrect as the expected output should be [1,2].
4) First bug fixed
So, what’s the solution?
After encountering the issue with the delete method, I opted for another approach to remove the value of `nums[i]`.
I found that utilizing the splice method allowed me to remove the entire element from the array. Here’s how it looked when implemented in code:
After encountering the issue with the delete method, I opted for another approach to remove the value of `nums[i]`.
I found that utilizing the splice method allowed me to remove the entire element from the array. Here’s how it looked when implemented in code:
var removeDuplicates = function(nums{
for(let i=0; i<nums.length; i++){
if(nums[i]===nums[i+1]){
nums.splice(i, 1);
};
};
5) Final bug fixed
Finally, I discovered one remaining bug. Upon submitting the solution to LeetCode, I received feedback that for the input `nums=[1,1,1]`, the function outputted the array `[1,1]`, which was incorrect because duplicates still remained.
Upon investigation, I realized that the function worked properly only
when there were at most two occurrences of the same number in the array.
This was due to the for loop iterating from the current index `i`, rather than from the beginning of the array. In our example with `[1,1,1]`, it compared the first `1` with the second `1`, removing one value and resulting in the array `[1,1]`. However, instead of comparing both `1`s, it compared the second `1` with the next value, which was empty. Consequently, it returned `[1,1]`, but the correct output should have been `[1]`.
To resolve this issue, I needed to set the value of `i` to `i-1` after deleting a value. This way, if a value was deleted, the loop would continue from the next value, but at the one before.
The final implementation of the code looks like this:
Upon investigation, I realized that the function worked properly only
when there were at most two occurrences of the same number in the array.
This was due to the for loop iterating from the current index `i`, rather than from the beginning of the array. In our example with `[1,1,1]`, it compared the first `1` with the second `1`, removing one value and resulting in the array `[1,1]`. However, instead of comparing both `1`s, it compared the second `1` with the next value, which was empty. Consequently, it returned `[1,1]`, but the correct output should have been `[1]`.
To resolve this issue, I needed to set the value of `i` to `i-1` after deleting a value. This way, if a value was deleted, the loop would continue from the next value, but at the one before.
The final implementation of the code looks like this:
var removeDuplicates = function(nums{
for(let i=0; i<nums.length; i++){
if(nums[i]===nums[i+1]){
nums.splice(i, 1);
i=-1
};
};
Final words
I hope you enjoyed reading this article.
If you have any questions or remarks feel free to contact me.
Mario
If you have any questions or remarks feel free to contact me.
Mario