Skip to content

Optimized Solution for Removing Duplicates from a Sorted Array #2892

Open
@ManishCody

Description

@ManishCody

class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;

    int k = 0;
    
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] != nums[k]) {
            k++;
            nums[k] = nums[i];
        }
    }
    
    return k + 1; // The length of the unique elements
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions