DEV Community

Debesh P.
Debesh P.

Posted on • Edited on

80. Remove Duplicates from Sorted Array II | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/solutions/7416211/best-solution-in-java-beats-200-on-time-7dil5


leetcode 80


Solution

class Solution {
    public int removeDuplicates(int[] nums) {
        int n = nums.length;
        int j = 2;
        for(int i=2; i<n; i++) {
            if(nums[i] != nums[j-2]) {
                nums[j] = nums[i];
                j++;
            }
        }

        return j;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)