26. Remove Duplicates from Sorted Array

Removal of duplicate elements from sorted array

·

1 min read

26. Remove Duplicates from Sorted Array

Problem 26.

Remove duplicate from sorted array

intuition.

We solve the problem in the optimized way . using for loop and else-if conditional statement

solution .

Firstly we traversed the whole array and then we check is first element is same to the next one then use continue if not equal then replace the index of array with k i.e i replaced by k which is new index of array.

here are the Source code try this .

class Solution {
    public int removeDuplicates(int[] nums) {
        int k = 0;
        for(int  i = 0;i<nums.length;i++){
            if(i<nums.length-1 && nums[i] == nums[i+1]){
                continue;
            }
                else{
                nums[k] = nums[i];
                k++;
            }  
        }
    return k;
   }
}