LeetCode #26 Remove Duplicates from Sorted Array Solution & Explanation

LeetCode Problem

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

LeetCode #26 Custom Judge
LeetCode #26 Examples
LeetCode#26 Constraints

Solution

One thing to keep in mind is we need to return an integer(k) but also remove duplicates of array, not only return the integer or remove duplicates.

C# Solution

Solution1

public class Solution {
    public int RemoveDuplicates(int[] nums) {
      
        int tmp = nums[0];
        int cnt = 1;
        
        for(int i = 1;i<nums.Length;i++)
        {
            if(nums[i]>tmp)
            {
                tmp = nums[i];
                nums[cnt] = tmp;
                cnt++;
            }
        }
        
        return cnt;
    }
}

Runtime : 154ms、137ms、147ms ≈ 90~99% faster

Java Solution

Solution1

class Solution {
    public int removeDuplicates(int[] nums) {
        int tmp = nums[0];
        int cnt = 1;
        
        for(int i = 1;i<nums.length;i++)
        {
            if(nums[i]>tmp)
            {
                tmp = nums[i];
                nums[cnt] = tmp;
                cnt++;
            }
        }
        
        return cnt;
    }
}

Runtime : 1ms、1ms、1ms

Switch C# Length to Java length, and all is done💪

Python3 Solution

Solution1

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        tmp = nums[0]
        cnt = 1
        
        for i in range (1,len(nums)):
            if nums[i]>tmp :
                tmp = nums[i]
                nums[cnt] = tmp
                cnt+=1
                
        return cnt

Runtime : 91ms、78ms、122ms

JavaScript Solution

Solution1

/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function(nums) {
    var tmp = nums[0];
    var cnt = 1;

    for(var i = 1;i<nums.length;i++)
    {
        if(nums[i]>tmp)
        {
            tmp = nums[i];
            nums[cnt] = tmp;
            cnt++;
        }
    }
    return cnt;
};

Runtime : 74ms、66ms、77ms


Submission Detail


Conclusion

🧡If my solution helps, that is my honor!

The problem link : Remove Duplicates from Sorted Array – LeetCode

🧡See Next Problem : LeetCode #27 Remove Element from Sorted Array Solution & Explanation – zyrastory Code & Food Research Center

Latest Post

Leave a Reply

Your email address will not be published. Required fields are marked *