80. Remove Duplicates from Sorted Array II


Approach

two pointer solution


Complexity

Time complexity: O(n)

Space complexity: o(1)

Code

class Solution {

    public int removeDuplicates(int[] nums) {

        if(nums.length <= 2){

            return nums.length;

        }

        int j = 2;

        for(int i = 2; i < nums.length; i++){

            if(nums[i] != nums[j-2]){

                nums[j] = nums[i];

                j++;

            }

        }

        return j;

    }

}

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix